2017-06-15 75 views
0

我在我的程序這兩個功能:PIL:Imageobject.save()繪製完全腐化後的圖像和藍精靈的輸出中

def depict_ph_increase(x,y,color, imobject): 
    program_print(color) 
    draw = PIL.ImageDraw.Draw(imobject) 
    draw.text((x, y),color,(255,255,255)) 
    imobject.save('tmp-out.gif') 
    im_temp = PIL.Image.open("tmp-out.gif")#.convert2byte() 
    im_temp = im_temp.resize((930, 340), PIL.Image.ANTIALIAS) 
    MAP_temp = ImageTk.PhotoImage(im_temp) 
    map_display_temp = Label(main, image=MAP_temp) 
    map_display_temp.image = MAP_temp # keep a reference! 
    map_display_temp.grid(row=4,column=2, columnspan=3) 

def read_temp_pixels(temperature_file, rngup, rngdown): 
    temp_image_object = PIL.Image.open(temperature_file) 
    (length, width) = get_image_size(temp_image_object) 
    (rngxleft, rngxright) = rngup 
    (rngyup,rngydown) = rngdown 
    print 'the length and width is' 
    print length, width 
    hotspots = 5; 
    for hotspot in range(0,hotspots): 
     color = "#ffffff" 
     while color == "#ffffff" or color == "#000000" or color == "#505050" or color == "#969696": 
      yc = random.randint(rngxleft, rngxright) 
      xc = random.randint(rngyup,rngydown) 
      color = convert_RGB_HEX(get_pixel_color(temp_image_object, xc, yc)) 
     depict_ph_increase(xc,yc,color, temp_image_object) 

底部一個叫頂一個。他們的工作是讀入這個圖像:enter image description here

然後隨機選擇幾個像素,抓住它們的顏色,並將顏色的十六進制值寫在上面。但是,當它重新顯示圖像,它給了我這個垃圾: enter image description here

靠近右上角的那些白色數字是其繪圖的十六進制值。它以某種方式從損壞的圖像中讀取值,儘管事實上我沒有收集值,直到我實際調用​​方法之後。有人可以向我解釋爲什麼它會破壞圖像嗎?

某些背景 - get_pixel_color()函數在程序中幾次使用,並且非常準確,它只是以某種方式從新損壞的圖像中讀取像素數據。此外,我在我的代碼中的其他位置做了類似的圖像閱讀(但不寫)。

如果有什麼我可以澄清的,或我想要看到的代碼的任何其他部分,請讓我知道。您也可以在我的github上查看完整的程序:https://github.com/jrfarah/coral/blob/master/src/realtime.py它應該是提交#29。

其他SO問題,我已經研究過了,沒有效果:Corrupted image is being saved with PIL

任何幫助將不勝感激!

回答

0

我編輯這行解決了這一問題:

temp_image_object = PIL.Image.open(temperature_file) 

temp_image_object = PIL.Image.open(temperature_file).convert('RGB')