2012-08-10 44 views
4

我想旋转图像,但我想保持图像的大小。例如在下面的示例图像中,我想看到一个完整的矩形,旋转后没有黑色背景色。Python的Django如何旋转图像并删除黑色?

请帮助我,我会很感激。现在

我的代码是:

src_im = Image.open("test.gif") 
im = src_im.rotate(30) 
im.save("result.gif") 

enter image description here enter image description here

+1

参见:http://stackoverflow.com/questions/5252170/specify-image-filling-color-when -python-with-pil-and-setting-expand – 2012-08-10 13:27:58

+0

谢谢@FrancisYaconiello它的工作! – 2012-08-10 14:06:03

回答

1

要自动调整,你想要的expand kwarg:

src_im = Image.open("test.gif") 
im = src_im.rotate(30, expand=True) 
im.save("result.gif") 

由于the PIL docs解释:

展开论点,如果属实,表示输出图像应该大到足以容纳旋转的图像。如果省略或为false,则输出图像的大小与输入图像的大小相同。

在黑色的背景,你保存GIF图像时需要specify the transparent colour

transparency = im.info['transparency'] 
im.save('icon.gif', transparency=transparency) 
+0

感谢@supervacuo的帮助,我现在可以做到。 – 2012-08-10 14:07:14

0

你没有带设置你的新图像的大小 - 如果你只有30度的图像需要扩大旋转.. 。

http://effbot.org/imagingbook/image.htm页:

扩展参数(如果为true)表示输出图像应该大到足以容纳旋转后的图像 。如果省略或错误,则输出图像的大小与输入图像的大小相同。

因此,尝试(这是键入到浏览器,以便未经):

src_im = Image.open("test.gif") 
im = src_im.rotate(30, expand=True) 
im.save("result.gif") 

对于黑色背景那是一个多一点tricky-因为你是90不旋转,有面积需要包含“某物”的正方形(尽管透明度可能是你想要的)。看看这个答案:Specify image filling color when rotating in python with PIL and setting expand argument to true

Goodluck!

+0

感谢@mchicago的帮助,这是工作。 – 2012-08-10 14:07:55

相关问题