旋转 PIL 图片好像没有旋转 canvas(没有添加 TKinter canvas)

Rotating PIL Image does not seem to rotate canvas (no TKinter canvas added)

我在旋转我创建的图像时遇到问题。因为代码分布在许多方法中,所以我将我认为是相关的命令放在下面。

问题是当图像创建成功时,当我使用 img.rotate(-90) 旋转它时...图像旋转了,但 pallet/background/canvas 似乎没有(见附图)。

我该如何纠正这个问题。我需要创建更大的透明背景吗?我能否让 background/canvas 也旋转...或者我旋转然后调整 background/Canvas 的大小?

第一个示例图片(二维码)

img = Image.new('RGB', (x,y), 'white')
qr = qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=1,)
qr.add_data('QRB.NO/AbCd1')
qr.make(fit=True)
QRimg = qr.make_image()
img = img.paste(QRimg, (x,y))
img.show() #333
raw_input('(Above is unrotated QRcode image) Press enter...') #333
img = img.rotate(-90)
print img, type(img)
img.show() #333
raw_input('Above is the rotated -90 QRcode image. Press enter...') #333

第二张示例图片

font_name      = 'Arial.ttf'
font_size      = 16 
font = ImageFont.truetype(font_name, font_size)
img = Image.new('RGB', (x,y), color=background_color)
# Place text 
draw = ImageDraw.Draw(img)
draw.text( (corner_X,corner_Y), 'QRB.NO/AbCd1', font=font, fill='#000000' ) 
draw.rectangle((0,0,x-1,y-1), outline = "black")
del draw
print img, type(img)
img.show() #333
raw_input('(Above is the unrotated test image). Press enter...') #333
img = img.rotate(90)
print img, type(img)
img.show() #333
raw_input('(Above is the ROTATED 90 text image). Press enter...') #333

输出

<PIL.Image.Image image mode=RGB size=71x57 at 0x10E9B8C10> <class 'PIL.Image.Image'>
(Above is unrotated QRcode image) Press enter...

<PIL.Image.Image image mode=RGB size=71x57 at 0x10E9B8F90> <class 'PIL.Image.Image'>
Above is the rotated -90 QRcode image. Press enter...

<PIL.Image.Image image mode=RGB size=57x9 at 0x10EA6CB90> <class 'PIL.Image.Image'>
(Above is the unrotated test image). Press enter...

<PIL.Image.Image image mode=RGB size=57x9 at 0x10E9B8C10> <class 'PIL.Image.Image'>
(Above is the ROTATED 90 text image). Press enter...


编辑:

x,y = img.size
img = img.resize( (y, x),  Image.ANTIALIAS )
img = img.rotate(-90)

...或者...

x,y = img.size
img = img.rotate(-90)
img = img.resize( (y, x),  Image.ANTIALIAS )

...似乎没有帮助。

想通了。我将保留它以帮助其他人,因为这似乎是一个微妙但重要的区别。

img = img.transpose(Image.ROTATE_270) 

...或者...

img = img.transpose(Image.ROTATE_90) 

Docs

在旋转方法中使用可选的 expand 标志:

image.rotate(45, expand=True)

https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.rotate