旋转PIL图像似乎没有旋转画布(没有添加TKinter画布)

Rig*_*reM 5 python image-processing rotation python-imaging-library

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

问题是,当图像成功创建时,当我使用img.rotate(-90)旋转它时...图像旋转,但托盘/背景/画布看起来没有(参见附图).

我怎么能纠正这个.我是否需要创建更大的透明背景?我可以让背景/画布也旋转......还是旋转然后调整背景/画布的大小?

第一个示例图像(QRCODE)

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
Run Code Online (Sandbox Code Playgroud)

第二个例子图像

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
Run Code Online (Sandbox Code Playgroud)

OUTPUT

<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...
Run Code Online (Sandbox Code Playgroud)


糟糕的轮换

编辑:

x,y = img.size
img = img.resize( (y, x),  Image.ANTIALIAS )
img = img.rotate(-90)
Run Code Online (Sandbox Code Playgroud)

...要么...

x,y = img.size
img = img.rotate(-90)
img = img.resize( (y, x),  Image.ANTIALIAS )
Run Code Online (Sandbox Code Playgroud)

......似乎没有帮助.

尝试调整大小并旋转

Rig*_*reM 13

弄清楚了.我将把它留给别人帮助,因为这似乎是一个微妙但重要的区别.

img = img.transpose(Image.ROTATE_270) 
Run Code Online (Sandbox Code Playgroud)

...要么...

img = img.transpose(Image.ROTATE_90) 
Run Code Online (Sandbox Code Playgroud)

文件


pri*_*ist 8

expand在rotate方法中使用可选标志:

image.rotate(45, expand=True)

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