我有一个旋转矩形的四个点的列表,格式如下:
points = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
我可以在PIL中使用以下方法进行剪裁:
img.crop((x1, y1, x2, y2))
但是这种方法无法处理旋转的矩形。为了澄清,我希望裁剪后的图像被旋转,使得裁剪区域成为一个非旋转的矩形。
我愿意使用openCV,尽管我想避免它,因为从PIL到openCV的图像转换需要时间,并且我将要重复执行大约100次这个过程。
我有一个旋转矩形的四个点的列表,格式如下:
points = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
img.crop((x1, y1, x2, y2))
但是这种方法无法处理旋转的矩形。为了澄清,我希望裁剪后的图像被旋转,使得裁剪区域成为一个非旋转的矩形。
我愿意使用openCV,尽管我想避免它,因为从PIL到openCV的图像转换需要时间,并且我将要重复执行大约100次这个过程。
#!/usr/bin/env python3
from PIL import Image, ImageTransform
# Open starting image and ensure RGB
im = Image.open('a.png').convert('RGB')
# Define 8-tuple with x,y coordinates of top-left, bottom-left, bottom-right and top-right corners and apply
transform=[31,146,88,226,252,112,195,31]
result = im.transform((200,100), ImageTransform.QuadTransform(transform))
# Save the result
result.save('result.png')