Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to CREATE a transparent gif (or png) with PIL (python-imaging)

Trying to create a transparent gif with PIL. So far I have this:

    from PIL import Image      img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))     img.save("test.gif", "GIF", transparency=0) 

Everything I've found so far refers to manipulating an existing image to adjust it's transparency settings or overlaying a transparent image onto another. I merely want to create a transparent GIF (to then draw onto).

like image 620
gratz Avatar asked Dec 04 '11 15:12

gratz


People also ask

How do I make pixels transparent in Python?

putpixel((x, y), (255, 255, 255, 0)) can make a pixel transparent.

How do I Pil an image?

Image. open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).


1 Answers

The following script creates a transparent GIF with a red circle drawn in the middle:

from PIL import Image, ImageDraw  img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))  draw = ImageDraw.Draw(img) draw.ellipse((25, 25, 75, 75), fill=(255, 0, 0))  img.save('test.gif', 'GIF', transparency=0) 

and for PNG format:

img.save('test.png', 'PNG') 
like image 178
ekhumoro Avatar answered Oct 22 '22 06:10

ekhumoro