2016-03-10 111 views
4

我遇到了旋转我创建的图像的问题。由于代码分散在多种方法之中,因此我已将下面的相关命令作为我的想法。旋转PIL图像似乎不旋转画布(未添加任何TKinter画布)

问题是,当图像被成功创建时,当我使用img.rotate(-90)旋转图像时...图像旋转,但它显示托盘/背景/画布不显示(请参阅附加图像)。

我该如何纠正这一点。我是否需要创建更大的透明背景?我可以让背景/画布旋转以及旋转,然后调整背景/画布的大小?

FIRST示例图像(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 

SECOND示例图像

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 

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... 


Bad Rotations

编辑:

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) 

...没有帮助。

Trying resize and rotate

回答

8

想通了。我会留下来帮助别人,因为这似乎是一个微妙而重要的区别。

img = img.transpose(Image.ROTATE_270) 

......或者......

img = img.transpose(Image.ROTATE_90) 

Docs