How to reduce the image file size using PIL

A built-in parameter for saving JPEGs and PNGs is optimize. from PIL import Image foo = Image.open(‘path/to/image.jpg’) # My image is a 200×374 jpeg that is 102kb large foo.size # (200, 374) # downsize the image with an ANTIALIAS filter (gives the highest quality) foo = foo.resize((160,300),Image.ANTIALIAS) foo.save(‘path/to/save/image_scaled.jpg’, quality=95) # The saved downsized image size … Read more

How to write PNG image to string with the PIL?

You can use the BytesIO class to get a wrapper around strings that behaves like a file. The BytesIO object provides the same interface as a file, but saves the contents just in memory: import io with io.BytesIO() as output: image.save(output, format=”GIF”) contents = output.getvalue() You have to explicitly specify the output format with the … 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

Working with TIFFs (import, export) in Python using numpy

First, I downloaded a test TIFF image from this page called a_image.tif. Then I opened with PIL like this: >>> from PIL import Image >>> im = Image.open(‘a_image.tif’) >>> im.show() This showed the rainbow image. To convert to a numpy array, it’s as simple as: >>> import numpy >>> imarray = numpy.array(im) We can see … Read more

Can’t install PIL after Mac OS X 10.9

Following worked for me: ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/X11 /usr/local/include/X11 sudo pip install pil UPDATE: But there is more correct solution below, provided by Will. open your terminal and execute: xcode-select –install

Convert RGBA PNG to RGB with PIL

Here’s a version that’s much simpler – not sure how performant it is. Heavily based on some django snippet I found while building RGBA -> JPG + BG support for sorl thumbnails. from PIL import Image png = Image.open(object.logo.path) png.load() # required for png.split() background = Image.new(“RGB”, png.size, (255, 255, 255)) background.paste(png, mask=png.split()[3]) # 3 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)