What is causing an error when opening a PPM image file in Python?

In summary, The user is trying to write a program for simple image editing using the graphics library and is encountering an error when trying to open a ppm file. They later discover that the issue is with the format indicator of the file and resolve it by changing the file to P6 format. They are now looking for a different way to declare an Image object for P3 format ppm files.
  • #1
ard12
2
0

Homework Statement


I'm writing a program that will do simple image editing with ppm files. I can only use the graphics library and things in there. No PIL or Image module.

My problem is that I get an error that says "couldn't recognize data in image file "sole.ppm"
I know for a fact it is a correct ppm file.

The Attempt at a Solution



Here's my code:

Code:
def main():
print("Image Editor")
print()
filename = input("name of image file: ")
print()

with open(filename) as f:
    formatind = f.readline()
    width, height = [int(x) for x in f.readline().split()]
    colordepth = f.readline()
    array = []
    for line in f:
        array.append([int(x) for x in line.split()])

win = GraphWin("Image Editor!", width, height)

image = Image(Point(100,100), filename)

Display(image, array, width, height, win)

inf.close()
win.getMouse()
win.close()

main()

And my display function looks like this:

Code:
def Display(image, array, width, height, win):

for i in range(width):
    for j in range(0, height, 3):
        colors = color_rgb(array[i][j], array[i][j+1], array[i][j+2])
        image.setPixel(i, j, colors)
        image.draw(win)

return

I just cannot figure out what's wrong with this. Any help would be great. Thanks
 
Technology news on Phys.org
  • #2
I've just figured out that it's because of the format indicator. My file is P3. The Image constructor I used is not compatible with P3. I changed my file to P6 and it recognized the data. So I need another way of declaring an Image object for P3 format ppm file. Any ideas there?
 

Related to What is causing an error when opening a PPM image file in Python?

1. What is a PPM image?

A PPM (Portable Pixmap) image is a popular image format used for storing and displaying digital images. It stores the image data in a binary format and can support both grayscale and color images.

2. What is a PPM image editor in Python?

A PPM image editor in Python is a software program that allows users to manipulate and edit PPM images using the Python programming language. It can perform tasks such as resizing, cropping, and applying filters to PPM images.

3. How do I install a PPM image editor in Python?

To install a PPM image editor in Python, you can use the pip package manager. Simply open your command line interface and type "pip install [editor name]" to install the desired editor. Alternatively, you can download the editor's source code and install it manually.

4. Can I use a PPM image editor in Python for batch editing?

Yes, most PPM image editors in Python have the capability to perform batch editing. This means you can apply the same edits to multiple images at once, saving you time and effort.

5. Are there any free PPM image editors in Python available?

Yes, there are several free PPM image editors in Python available for download. Some popular options include Pillow, PyPNG, and Wand. These editors are open-source and can be used for personal and commercial purposes.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
2
Replies
41
Views
4K
  • Programming and Computer Science
Replies
4
Views
11K
  • Programming and Computer Science
Replies
2
Views
3K
Replies
2
Views
4K
Back
Top