Plot rotated images around a pivot Python

328 views Asked by At

I'm trying to plot an image that must be rotated around its corner in Python.

I could do a script that (kind of) do that job, but not around a pivot point as I wish:

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 that I've got was:

The result I've got

The result I wish is:

The result I wish

1

There are 1 answers

0
scleronomic On BEST ANSWER

You can use the transform keyword to define a rotation about a certain point:

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