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

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’)

Decoding base64 from POST to use in PIL

You should try something like: from PIL import Image from io import BytesIO import base64 data[‘img’] = ”’R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLl N48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==”’ im = Image.open(BytesIO(base64.b64decode(data[‘img’]))) Your data[‘img’] string should not include the HTML tags or the parameters data:image/jpeg;base64 that are in the example JSFiddle. I’ve changed the image string for an example I took from Google, just for … Read more

PIL “IOError: image file truncated” with big images

I’m a little late to reply here, but I ran into a similar problem and I wanted to share my solution. First, here’s a pretty typical stack trace for this problem: Traceback (most recent call last): … File …, line 2064, in … im.thumbnail(DEFAULT_THUMBNAIL_SIZE, Image.ANTIALIAS) File “/Library/Python/2.7/site-packages/PIL/Image.py”, line 1572, in thumbnail self.load() File “/Library/Python/2.7/site-packages/PIL/ImageFile.py”, line … Read more

Add Text on Image using PIL

I think ImageFont module available in PIL should be helpful in solving text font size problem. Just check what font type and size is appropriate for you and use following function to change font values. # font = ImageFont.truetype(<font-file>, <font-size>) # font-file should be present in provided path. font = ImageFont.truetype(“sans-serif.ttf”, 16) So your code … Read more