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

  • Thread starter Thread starter ard12
  • Start date Start date
  • Tags Tags
    Image Python
Click For Summary
The discussion centers on a programming issue related to image editing with PPM files, specifically encountering an error when trying to open a P3 format file using a graphics library. The user initially receives an error stating "couldn't recognize data in image file 'sole.ppm'," despite the file being valid. The code provided attempts to read the PPM file and display it using a graphical window. The user identifies that the problem stems from the format indicator; the graphics library's Image constructor does not support the P3 format, which is a text-based representation, while it recognizes the binary P6 format. The user successfully resolves the issue by converting the file to P6, but seeks alternative methods to create an Image object for P3 format files. The discussion highlights the limitations of the graphics library in handling different PPM formats and the need for a workaround to support P3 files.
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?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
5K
  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
2
Views
3K