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

  • Context: Python 
  • Thread starter Thread starter ard12
  • Start date Start date
  • Tags Tags
    Image Python
Click For Summary
SUMMARY

The discussion centers on a Python program for editing PPM image files using a graphics library, specifically encountering an error when opening a P3 format file. The error message "couldn't recognize data in image file 'sole.ppm'" indicates compatibility issues with the Image constructor. The user resolved the issue by converting the file to P6 format, which is compatible. The key takeaway is the need for a different method to handle P3 format files in the graphics library.

PREREQUISITES
  • Understanding of PPM image file formats (P3 and P6)
  • Familiarity with Python programming and syntax
  • Knowledge of the graphics library used for image manipulation
  • Basic understanding of image data structures and pixel manipulation
NEXT STEPS
  • Research how to create an Image object for P3 format PPM files in the graphics library
  • Explore the differences between P3 and P6 PPM formats
  • Learn about error handling in Python for file operations
  • Investigate alternative libraries for image processing in Python without using PIL
USEFUL FOR

This discussion is beneficial for Python developers, particularly those focused on image processing and manipulation, as well as students working on projects involving PPM files and graphics libraries.

ard12
Messages
2
Reaction score
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
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?
 

Similar threads

Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
2
Views
3K