围绕枢轴Python绘制旋转的图像

时间:2020-08-28 12:05:34

标签: python matplotlib rotation pivot python-imaging-library

我正在尝试绘制必须在Python中绕其角旋转的图像。

我可以做一个(可以做)这项工作的脚本,但不要像我希望的那样绕枢轴点:

import matplotlib.pyplot as plt
import PIL

picture= PIL.Image.open('images/sample-image.png')
maxsize = (40*10,30*10)
picture = picture.resize(maxsize,PIL.Image.ANTIALIAS)

comp = picture.im.size[0]
larg = picture.im.size[1]

plt.figure(figsize=(10,5),dpi=300)

plt.xlim((-100,500))
plt.ylim((-100,500))
plt.imshow(picture.rotate(0,expand=True))
plt.imshow(picture.rotate(-15,expand=True))
plt.imshow(picture.rotate(-30,expand=True))
plt.imshow(picture.rotate(-45,expand=True))
plt.imshow(picture.rotate(-90,expand=True))

plt.show()

我得到的结果是:

The result I've got

我希望的结果是:

The result I wish

1 个答案:

答案 0 :(得分:1)

您可以使用transform关键字来定义围绕某个点的旋转:

import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
import PIL

picture = PIL.Image.open('.../Downloads/puzzle.png')
maxsize = (50*10, 50*10)
picture = picture.resize(maxsize, PIL.Image.ANTIALIAS)

plt.figure(figsize=(10, 5), dpi=300)
plt.xlim((-600, 600))
plt.ylim((-600, 600))
plt.imshow(picture, transform=Affine2D().rotate_deg_around(*(0, 0), 0) + plt.gca().transData)
plt.imshow(picture, transform=Affine2D().rotate_deg_around(*(0, 0), 90) + plt.gca().transData)
plt.imshow(picture, transform=Affine2D().rotate_deg_around(*(0, 0), 180) + plt.gca().transData)
plt.imshow(picture, transform=Affine2D().rotate_deg_around(*(0, 0), 270) + plt.gca().transData)

enter image description here

Image source