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?
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top