from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC) img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True)from PIL import Image, ImageDraw #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) 现在已知在旋转前的图片gray2上有对应的点集合points,对点集points进行平移和旋转变换,使得其能在旋转后图片上完全重合的代码
时间: 2023-06-19 10:05:56 浏览: 98
可以参考以下代码:
```python
from PIL import Image, ImageDraw
import numpy as np
# 原始图片
gray2 = Image.fromarray(src)
# 中心点和旋转角度
max_point = (100, 100) # 假设最大点为(100, 100)
point2 = (200, 200) # 假设另一个点为(200, 200)
center = (max_point[0], max_point[1])
angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi
# 平移和旋转变换
width, height = gray2.size
img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC)
img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True)
# 裁剪
width1, height1 = gray2.size
width2, height2 = img_translated_rotated.size
left = (width2 - width1 )/2
top = (height2 - height1 )/2
right = (width2 - width1 )/2 + width1
bottom = (height2 - height1 )/2 + height1
cropped_image = img_translated_rotated.crop((left, top, right, bottom))
```
其中,假设最大点为(100, 100)、另一个点为(200, 200),可以根据实际情况修改。
阅读全文