Search code examples
pythonimageimportpython-imaging-libraryppm

Importing PPM images with python and PIL module


I need a way I can read the lines and extract the pixel info into some structure so I can use the putpixel function to create an image based on the ppm p3 file.

I'm working with Python Imaging Library (PIL) and I want to open a PPM image and display it as an image on the screen.

How can I do that using only PIL?

This is my ppm image. It's just a 7x1 image that I created.

P3
# size 7x1
7 1
255
0
0
0
201
24
24
24
201
45
24
54
201
201
24
182
24
201
178
104
59
14

Solution

  • edit: After you modified your question and you allow just reading the lines, check the link below. It explains how to write a wrapper that loads the file. I am about to test this myself and it should work...


    You currently (11/2010) cannot open plain PPM images with PIL. Plain here means ascii. Binary versions work however. The main reason for this is that ascii files do not have constant number of bits per pixel. And this is what the image loader in PIL assumes. I have a related question at:

    How to write PIL image filter for plain pgm format?

    I am planning to write a PIL-filter for plain PPM's but I am short on time. If you are interested to help, please let me know.

    br,
    Juha