def skew_image(img, angle):
"""
Skew image using some math
:param img: PIL image object
:param angle: Angle in radians (function doesn't do well outside the range -1 -> 1, but still works)
:return: PIL image object
"""
width, height = img.size
# Get the width that is to be added to the image based on the angle of skew
xshift = tan(abs(angle)) * height
new_width = width + int(xshift)
if new_width < 0:
return img
# Apply transform
img = img.transform(
(new_width, height),
Image.AFFINE,
(1, angle, -xshift if angle > 0 else 0, 0, 1, 0),
Image.BICUBIC
)
return img
评论列表
文章目录