3

I'm writing a simple picture editor. It uses PPM files. From what I can tell, I feel like my code should work. However, I get this error

Traceback (most recent call last):
  File "/home/zach/Downloads/piceditor (1).py", line 84, in <module>
    main()
  File "/home/zach/Downloads/piceditor (1).py", line 69, in main
    image = Image(Point(100,100), filename)
  File "/home/zach/Downloads/graphics.py", line 770, in __init__
    self.img = tk.PhotoImage(file=pixmap[0], master=_root)
  File "/usr/lib/python3.1/tkinter/__init__.py", line 3272, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.1/tkinter/__init__.py", line 3228, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "pig.ppm"

My code looks like this

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

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

This is the ppm file i'm using

P3
6 8
255
249 249 249 255 255 255 250 250 250 255 255 255 250 250 250 250 250 250 254 255 255 251 255 255 
249 251 255 253 249 255 255 248 255 255 234 255 255 242 255 255 245 253 255 246 243 255 253 241 
255 255 237 255 255 237 252 255 241 249 255 246 249 255 253 254 255 255 255 252 255 255 248 241 
255 251 239 254 247 241 252 254 253 252 255 255 251 255 255 242 242 242 255 255 255 241 241 241 
0 0 0 0 0 0 4 4 4 20 20 20 236 236 236 252 252 252 254 255 253 248 255 250 
0 0 0 0 0 0 4 4 4 20 20 20 236 236 236 252 252 252 254 255 253 248 255 250 

I cannot for the life of me figure out why it won't recognize the data in the file.

Any help would be great. Thanks

zburns12
  • 1,783
  • 3
  • 15
  • 16
  • are you absolutely certain your .ppm file is actually a proper .ppm file? Can you post the data? – Bryan Oakley Apr 26 '12 at 20:37
  • Your file says it's 6x8 but it's laid out 8x6. – Mark Ransom Apr 26 '12 at 21:39
  • 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? – zburns12 Apr 26 '12 at 21:57

1 Answers1

0

Why don't you use the PIL library? In the documents it claims that it can work with PPM files. However I am not familiar with working with PPM files with PIL.

Example: Opening a a PPM file, creating an object from the file that then can be used to edit the file.

Onlyjus
  • 5,799
  • 5
  • 30
  • 42