2017-06-15 27 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')