Skip to content

Image.rotate(90*n, expand=True) results in translation and expansion #1917

@BryghtShadow

Description

@BryghtShadow

What did you do?

Apply rotation of 90*N degrees to an image, with expand set to True.

What did you expect to happen?

Because the rotation is a multiple of 90 degrees, I expected the image to be rotated with no translations. I expected the result to match a rotation via Image.transpose.

What actually happened?

  • For 0 degrees, no visible difference (Working as intended)
  • For -90 degrees, the image is translated by (1+x, 1+y), but expands only in height. This results in the right-most column being cropped.
  • For +90 degrees, the image is translated by (1+x, 1+y), but expands only in width. This results in the bottom-most row being cropped.
  • For ±90*(2, 3, ..., N-1, N) degrees, the image is translated by (1+x, 1+y), and expands to fit the image.

What versions of Pillow and Python are you using?

Pillow 3.2.0; Python 3.4.4 and Python 3.5.1

from PIL import Image, ImageDraw

im = Image.new('RGBA', (11,13), 'white')
# Add markers to corners to check for any cropping
w, h = im.size
draw = ImageDraw.Draw(im)
draw.line((0, 0, 1, 1), fill='cyan')
draw.line((0, h-1, 1, h-2), fill='magenta')
draw.line((w-1, h-1, w-2, h-2), fill='yellow')
draw.line((w-1, 0, w-2, 1), fill='black')
del draw

im.show()

print('  deg :  old  ->  new')
for i in range(-4, 5):
    deg = i * 90
    im2 = im.rotate(deg, expand=True)
    print('{:>5} : {}x{} -> {}x{}'.format(deg, im.width, im.height, im2.width, im2.height))
    im2.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions