Reputation: 2259
I am trying out the Python Imaging Library, but `Image.rotate() seems to crop the image:
from PIL import Image
fn='screen.png'
im = Image.open(fn)
im = im.rotate(-90)
im = im.rotate(90)
im.show()
im.save('cropped.png')
The original image screen.png
is:
The cropped image cropped.png
is:
The cropping is caused by the 1st rotation and retained in the 2nd rotation.
I am still new to Python, despite trying to become more familiar with it over the years. Is this me doing something wrong with the PIL or is it a bug?
My Python version is:
Python 3.8.8 (default, Mar 4 2021, 21:24:42)
[GCC 10.2.0] on cygwin
Upvotes: 4
Views: 4576
Reputation: 2259
Thanks to pippo1980, the solution is to specify expand=1
when invoking rotate()
:
from PIL import Image
fn='screen.png'
im = Image.open(fn)
im = im.rotate(-90,expand=1)
im = im.rotate(90,expand=1)
im.show()
im.save('cropped.png')
It adjusts the "outer image" width and height to allow an arbitrarily rotated image to fit without cropping. Despite the name, it also shrinks width/height where appropriate.
Upvotes: 11