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
AI Thread 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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top