Questions tagged [python-imaging-library]

The Python Imaging Library (PIL) provides the Python language with a de-facto standard foundation for image work. PIL’s API is lightweight but semantically consistent; it furnishes a range of comfortably Pythonic tools throughout much of the imaging lexicon: processing, analysis, compression and codec abstraction, etc. – all of which builds upon a bespoke and readily extensible library structure.

The Python Imaging Library (PIL) provides the Python language with a de-facto standard foundation for image work.

PIL’s API is lightweight but semantically consistent. It furnishes a range of comfortably Pythonic tools, whose top-level hierarchy covers a broad lexical range of the imaging fields’ usual suspects, including: pixel and channel processing; statistical analysis; affine, Gaussian, and kernel transforms, abstraction for compressors, binary formats, codecs, and the like; vector drawing and typesetting; pixel mathematics and color correction; matrices; programming and I/O paradigms. Each of these is notionally organized into a Python sub-package and implemented with an API in a mode whose form is germane to its concept – e.g. while the IO modules like PIL.Image and PIL.ImageFile generally follow along with the Python standard libraries — they employ OO-style conventions that very similar throughout – PIL is not pedantic in this way, as other submodules are non-uniform when they need to be… PIL.ImageFilter, for example, is written in a functional style (obfuscated slightly, due to having been CamelCased). And PIL.ImageMath exposes its interface as a little subset of Python language itself, with which the programmer can tersely and legibly describe an algorithm – which PIL can then take and deterministically pipeline and vectorize into something much faster than naïve Python.However, it only supports Python 1.5.2 and newer, including 2.5 and 2.6.

More information can be found at PIL (PythonWare.com); founded and first developed by Fredrik Lundh, the original PIL codebase is open source, but unmaintained since 2011.

There is also an active, maintained and popular fork of PIL called Pillow which releases quarterly with security updates, new features and bug fixes, it supports python 3.x.

9873 questions
115
votes
6 answers

How do you composite an image onto another image with PIL in Python?

I need to take an image and place it onto a new, generated white background in order for it to be converted into a downloadable desktop wallpaper. So the process would go: Generate new, all white image with 1440x900 dimensions Place existing image…
Sebastian
  • 2,470
  • 4
  • 21
  • 27
114
votes
11 answers

Center-/middle-align text with PIL?

How would I center-align (and middle-vertical-align) text when using PIL?
Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134
102
votes
7 answers

PIL "IOError: image file truncated" with big images

I think this problem is not Zope-related. Nonetheless I'll explain what I'm trying to do: I'm using a PUT_factory in Zope to upload images to the ZODB per FTP. The uploaded image is saved as a Zope Image inside a newly created container object. This…
Georg Pfolz
  • 1,416
  • 2
  • 12
  • 12
99
votes
10 answers

How to use PIL to make all white pixels transparent?

I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle) I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the…
haseman
  • 11,213
  • 8
  • 41
  • 38
95
votes
5 answers

Getting "cannot write mode P as JPEG" while operating on JPG image

I am trying to resize some images, most of which are JPG. But in a few images, I am getting the error: Traceback (most recent call last): File "image_operation_new.py", line 168, in modifyImage tempImage.save(finalName); File…
vlad halmer
  • 1,211
  • 2
  • 16
  • 21
90
votes
6 answers

Using PIL to turn a RGB image into a pure black and white image

I'm using the Python Imaging Library for some very simple image manipulation, however I'm having trouble converting a greyscale image to a monochrome (black and white) image. If I save after changing the image to greyscale (convert('L')) then the…
user714852
  • 2,054
  • 4
  • 30
  • 52
90
votes
6 answers

Resampling a numpy array representing an image

I am looking for how to resample a numpy array representing image data at a new size, preferably having a choice of the interpolation method (nearest, bilinear, etc.). I know there is scipy.misc.imresize which does exactly this by wrapping PIL's…
Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
89
votes
12 answers

Multiplying a tuple by a scalar

I have the following code: print(img.size) print(10 * img.size) This will print: (70, 70) (70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70) I'd like it to print: (700, 700) Is there any way to do this without having…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
88
votes
4 answers

Convert png to jpeg using Pillow

I am trying to convert png to jpeg using pillow. I've tried several scrips without success. These 2 seemed to work on small png images like this one. First code: from PIL import Image import os, sys im = Image.open("Ba_b_do8mag_c6_big.png") bg =…
alex
  • 2,381
  • 4
  • 23
  • 49
88
votes
4 answers

Convert opencv image format to PIL image format?

I want to convert an image loaded TestPicture = cv2.imread("flowers.jpg") I would like to run a PIL filter like on the example with the variable TestPicture but I'm unable to convert it back and forth between these types. Is there a way to do…
87
votes
6 answers

PIL TypeError: Cannot handle this data type

I have an image stored in a numpy array that I want to convert to PIL.Image in order to perform an interpolation only available with PIL. When trying to convert it through Image.fromarray() it raises the following error: TypeError: Cannot handle…
Jerome
  • 983
  • 1
  • 6
  • 4
85
votes
16 answers

ImportError: cannot import name _imaging

I installed Pillow, and after I want to do: from PIL import Image I get the following error: Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 61, in…
tomooka
  • 1,091
  • 2
  • 9
  • 12
84
votes
2 answers

how to save a pylab figure into in-memory file which can be read into PIL image?

The following is my first shot which never works: import cStringIO import pylab from PIL import Image pylab.figure() pylab.plot([1,2]) pylab.title("test") buffer = cStringIO.StringIO() pylab.savefig(buffer, format='png') im =…
nye17
  • 12,857
  • 11
  • 58
  • 68
84
votes
14 answers

PIL thumbnail is rotating my image?

I'm attempting to take large (huge) images (from a digital camera), and convert them into something that I can display on the web. This seems straightforward, and probably should be. However, when I attempt to use PIL to create thumbnail versions,…
Hoopes
  • 3,943
  • 4
  • 44
  • 60
82
votes
3 answers

Get error when try to install PIL

I try to install PIL but get errors, what should I do? $ Command Result ------------ $ pip install PIL Collecting PIL Could not find a version that satisfies the requirement PIL (from versions: ) No matching distribution found for…
Shira
  • 823
  • 1
  • 6
  • 8