The problem with installing PIL using virtualenv or buildout

The PIL version packaged on pypi (by the author) is incompatible with setuptools and thus not easy_installable. People have created easy_installable versions elsewhere. Currently, you need to specify a find-links URL and use pip get a good package: pip install –no-index -f http://dist.plone.org/thirdparty/ -U PIL By using pip install with the –no-index you avoid running … Read more

How to convert a NumPy array to PIL image applying matplotlib colormap

Quite a busy one-liner, but here it is: First ensure your NumPy array, myarray, is normalised with the max value at 1.0. Apply the colormap directly to myarray. Rescale to the 0-255 range. Convert to integers, using np.uint8(). Use Image.fromarray(). And you’re done: from PIL import Image from matplotlib import cm im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255)) with … Read more

How do I generate circular thumbnails with PIL?

The easiest way to do it is by using masks. Create a black and white mask with any shape you want. And use putalpha to put that shape as an alpha layer: from PIL import Image, ImageOps mask = Image.open(‘mask.png’).convert(‘L’) im = Image.open(‘image.png’) output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) output.save(‘output.png’) Here is the mask … Read more