Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django how to rotate image and remove black color?

I am trying to rotate image, but I want to maintain the size of the Image. For example in the following example image, I want to see a complete rectangle and have no black background color after rotating.

Please help me, I'd appreciate it.

Now my code is:

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

enter image description hereenter image description here

like image 597
Apple Wang Avatar asked Oct 07 '22 23:10

Apple Wang


2 Answers

To resize automatically, you want the expand kwarg:

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

As the PIL docs explain:

The expand argument, if true, indicates that the output image should be made large enough to hold the rotated image. If omitted or false, the output image has the same size as the input image.

On the black background, you need to specify the transparent colour when saving your GIF image:

transparency = im.info['transparency'] 
im.save('icon.gif', transparency=transparency)
like image 166
supervacuo Avatar answered Oct 19 '22 21:10

supervacuo


try this one:

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

here comes the results:

test.jpg result.gif

like image 44
Alpha Avatar answered Oct 19 '22 21:10

Alpha