2012-04-26 128 views
3

我在寫一個簡單的圖片編輯器。它使用PPM文件。從我所知道的,我覺得我的代碼應該工作。不過,我得到這個錯誤無法識別PPM文件數據

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" 

我的代碼看起來像這樣

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() 

我的顯示功能看起來像這樣

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 

這是PPM文件我使用

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 

我不能t他的生活找出爲什麼它不會識別文件中的數據。

任何幫助將是偉大的。謝謝

+0

你是否確定你的.ppm文件實際上是一個合適的.ppm文件?你可以發佈數據嗎? – 2012-04-26 20:37:42

+0

這裏是我正在使用的文件 – zburns12 2012-04-26 20:42:44

+0

你的文件說它是6x8,但它的佈局是8x6。 – 2012-04-26 21:39:31

回答

0

爲什麼不使用PIL庫?在文件中它聲稱它可以與PPM文件一起使用。但是我不熟悉用PIL處理PPM文件。

Example:打開一個PPM文件,從文件中創建一個對象,然後用它來編輯該文件。