Why is the quality of JPEG images produced by PIL so poor?

There are two parts to JPEG quality. The first is the quality setting which you have already set to the highest possible value. JPEG also uses chroma subsampling, assuming that color hue changes are less important than lightness changes and some information can be safely thrown away. Unfortunately in demanding applications this isn’t always true, … Read more

AttributeError: module ‘PIL.Image’ has no attribute ‘ANTIALIAS’

ANTIALIAS was removed in Pillow 10.0.0 (after being deprecated through many previous versions). Now you need to use PIL.Image.LANCZOS or PIL.Image.Resampling.LANCZOS. (This is the exact same algorithm that ANTIALIAS referred to, you just can no longer access it through the name ANTIALIAS.) Reference: Pillow 10.0.0 release notes (with table of removed constants) Simple code example: … Read more

Converting a NumPy array to a PIL image

The RGB mode is expecting 8-bit values, so just casting your array should fix the problem: In [25]: image = Image.fromarray(pixels.astype(‘uint8’), ‘RGB’) …: …: # Print out the pixel values …: print image.getpixel((0, 0)) …: print image.getpixel((0, 1)) …: print image.getpixel((1, 0)) …: print image.getpixel((1, 1)) …: (255, 0, 0) (0, 0, 255) (0, 255, … Read more

How can I install PIL on mac os x 10.7.2 Lion

If you use homebrew, you can install the PIL with just brew install pil. You may then need to add the install directory ($(brew –prefix)/lib/python2.7/site-packages) to your PYTHONPATH, or add the location of PIL directory itself in a file called PIL.pth file in any of your site-packages directories, with the contents: /usr/local/lib/python2.7/site-packages/PIL (assuming brew –prefix … Read more

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

How I can load a font file with PIL.ImageFont.truetype without specifying the absolute path?

To me worked this on xubuntu: from PIL import Image,ImageDraw,ImageFont # sample text and font unicode_text = u”Hello World!” font = ImageFont.truetype(“/usr/share/fonts/truetype/freefont/FreeMono.ttf”, 28, encoding=”unic”) # get the line size text_width, text_height = font.getsize(unicode_text) # create a blank canvas with extra space between lines canvas = Image.new(‘RGB’, (text_width + 10, text_height + 10), “orange”) # draw … Read more