Importing PPM images with python and PIL module

↘锁芯ラ 提交于 2021-02-04 21:00:16

问题


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

回答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




回答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)



回答3:


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

The very first example

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



回答4:


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?




回答5:


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.




回答6:


im = Image.open("lena.ppm")

This doesn't seem to work for a P3 *.PPM, it works if you try a P6 one.



来源:https://stackoverflow.com/questions/4101576/importing-ppm-images-with-python-and-pil-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!