Importing PPM images with python and PIL module

25,886

Solution 1

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

Solution 2

And if you like working with np.array objects, just do this:

>>> from scipy.misc import imread
>>> img = imread(path_to_ppm_file)
>>> img.shape
>>> (234, 555, 3)

Solution 3

Read the tutorial: http://effbot.org/imagingbook/introduction.htm

The very first example

>>> import Image
>>> im = Image.open("lena.ppm")
>>> im.show()

Solution 4

Some background concept👨‍🏫 using your exact example:

  • .ppm is one of the file formats in which image data is stored so that it is more human🕴 readable.

  • It stands for Portable PixMap format

  • These files are usually of the following format:

# Optional Comments likes this one
# The first line is the image header which contains the format followed by width and height
P3 7 1
# Second line contains the maximum value possible for each color point
255
# Third line onwards, it contains the pixels represented in rows(7) and columns(1)
0 0 0
201 24 24 
24 201 45 
24 54 201
201 24 182 
24 201 178 
104 59 14

Reference

So you can see that you have properly rewrite your PPM file (since RGB triplets are considered for each pixel in a color image)

Opening📬 and visualizing🔭 the file

OpenCV (Does a fantastic job)

import cv2
import matplotlib.pyplot as plt
img = cv2.imread("\path to the image")
# Remember, opencv by default reads images in BGR rather than RGB
# So we fix that by the following
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# Now, for small images like yours or any similar ones we use for example purpose to understand image processing operations or computer graphics
# Using opencv's cv2.imshow()
# Or google.colab.patches.cv2_imshow() [in case we are on Google Colab]
# Would not be of much use as the output would be very small to visualize
# Instead using matplotlib.pyplot.imshow() would give a decent visualization
plt.imshow(img)

Pillow (or as we call it as PIL)

Although the documentation states that we can directly open .ppm files using🤨:

from PIL import Image
img = Image.open("path_to_file")

Reference

However, when we inspect further we can see that they only support the binary version (otherwise called P6 for PPM)😫 and not the ASCII version (otherwise called P3 for PPM)😑.

Reference

Hence, for your use case using PIL would not be an ideal option❌.

The benefit of visualization🔭 using matplotlib.pyplot.imshow() shall hold true as above.

Solution 5

Edit: A little more information goes a long way. Now that I see the image you're trying to open along with the exact error message, I remember a little-documented fact about PIL and PPM - PIL doesn't support the ASCII versions starting with P1/P2/P3, only the binary versions P4/P5/P6. P.S. You're missing a field in your file, there should be a 255 for the maximum pixel value after the width and height.


PPM is listed as a supported format, you should be able to open the file with Image.open('myfile.ppm').

A little more information is required to display the image. What OS are you using, and do you have a preference for the window functions you'd like to use?

Share:
25,886
andrepcg
Author by

andrepcg

Informatics Engineering Student @ University of Coimbra

Updated on February 05, 2021

Comments

  • andrepcg
    andrepcg over 3 years

    EDIT: Actually, 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've been trying this for so long and I can't just get it right.

    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