Skip to content
Igor Karpov edited this page Apr 26, 2015 · 1 revision

Installing PIL

To install PIL, download the latest version (1.1.7) for your OS (linux, windows, MacOSX).

In linux, install with:

tar xzf Imaging-1.1.7.tar.gz
cd Imaging-1.1.7
python setup.py build
python setup.py install

To test your installation of PIL, download a PNG image file such as this stapler:

import Image
im = Image.open("hw5_stapler.png")
print im.format, im.size, im.mode
# >> PNG (600, 468) RGBA
bw = im.convert("L")
bw.save("hw5_bw_stapler.png", "PNG")

The resulting file should look like this black and white version of the stapler.

If you want to create a new, blank image with the same dimensions and color mode as the black and white photo but perhaps with some custom values based on calculations stored in an array (e.g., if you have an array of edge pixels and want to create an image with them):

im = Image.new(screen.mode, screen.size)
im_pixels = im.load()
# iterate over your array and set the image pixels with im_pixels[x,y] = ...
im.save("image.png", "PNG")

Keep in mind that in a black and white photo, the values for each pixel are in the range [0,255]. For more information about using PIL with the Image class, see the online handbook.

Clone this wiki locally