使用PIL的ImageDraw模块

rec*_*gle 5 python image-manipulation tkinter image-processing python-imaging-library

我正在尝试使用PIL的ImageDraw模块进行单独的像素操作.下面的代码应该创建Tkinter canvas小部件.然后打开图像,将一个像素的颜色更改为红色,然后将图像嵌入到画布小部件中.但是,它似乎没有起作用.

我的代码:

import Tkinter
from PIL import ImageTk, Image, ImageDraw


class image_manip(Tkinter.Tk):

    def __init__(self):
        Tkinter.Tk.__init__(self)

        self.configure(bg='red')

        self.ImbImage = Tkinter.Canvas(self, highlightthickness=0, bd=0, bg='blue')
        self.ImbImage.pack()

        im = Image.open(r'C:\Python26\Suite\test.png')

        print im.format, im.size, im.mode

        im = ImageDraw.Draw(im)

        im = im.point((0, 0), fill="red")

        self.i = ImageTk.PhotoImage(im)
        self.ImbImage.create_image(139, 59, image=self.i)




def run():
    image_manip().mainloop()
if __name__ == "__main__":
    run()
Run Code Online (Sandbox Code Playgroud)

运行我的代码时出现以下错误:

Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage instance at 0x05DF7698>> ignored
Traceback (most recent call last):
  File "<string>", line 245, in run_nodebug
  File "C:\Python26\Suite\test_image.py", line 30, in <module>
    run()
  File "C:\Python26\Suite\test_image.py", line 28, in run
    image_manip().mainloop()
  File "C:\Python26\Suite\test_image.py", line 20, in __init__
    self.i = ImageTk.PhotoImage(im)
  File "C:\Python26\lib\site-packages\PIL\ImageTk.py", line 109, in __init__
    mode = Image.getmodebase(mode)
  File "C:\Python26\lib\site-packages\PIL\Image.py", line 245, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "C:\Python26\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
    return _modes[mode]
KeyError: None
Run Code Online (Sandbox Code Playgroud)

mpe*_*kov 9

你的问题是你要重新分配im多件事.

im = Image.open(r'C:\Python26\Suite\test.png')
im = ImageDraw.Draw(im)
im = im.point((0, 0), fill="red")
Run Code Online (Sandbox Code Playgroud)

当你调用时ImageTk.PhotoImage(im),函数需要一个PIL图像对象,但是你已经分配impoint()实际返回的函数的结果None.这是导致问题的原因.

我认为你误解了ImageDraw的工作原理.看看这里的例子.基本上:

  • ImageDraw如果要在PIL图像上绘制复杂的东西,则需要一个实例
  • 您仍然需要将PIL图像保留在某个变量中
  • ImageDraw 直接在施工期间给出的图像上绘画
  • 你可以随时扔掉ImageDraw物体.它不包含任何重要信息,因为所有内容都直接写入图像.

这是固定的__init__方法:

def __init__(self):
    Tkinter.Tk.__init__(self)
    self.configure(bg='red')
    im = Image.open(r'C:\Python26\Suite\test.png')
    width, height = im.size
    self.ImbImage = Tkinter.Canvas(self, highlightthickness=0, bd=0, bg='red', width=width, height=height)
    self.ImbImage.pack()
    print im.format, im.size, im.mode

    draw = ImageDraw.Draw(im)
    draw.rectangle([0, 0, 40, 40 ],  fill="green")
    del draw

    self.i = ImageTk.PhotoImage(im)
    self.ImbImage.create_image(width/2, height/2, image=self.i)
Run Code Online (Sandbox Code Playgroud)

你会注意到我修复了一些事情:

  • 将画布大小设置为图像的大小.显然,你需要在找到图像大小之前加载图像,所以我已经移动了一些东西.
  • ImageDraw实例分配给单独的变量
  • 绘制一个绿色矩形而不是一个点,因为这将更突出.请注意,您不需要像其他大多数绘图函数那样获取返回值draw.rectangle- 它实际返回None.
  • draw完成绘图后删除变量
  • 调用时将图像置于画布中心 create_image

  • 请注意,您必须将 ImageTk.PhotoImage(im) 分配给*持久*(非垃圾收集)变量。Tkinter 中有一个错误,如果您只是将图像分配给局部变量并在 create_image 中使用它,图像将被垃圾收集。请参阅http://infohost.nmt.edu/tcc/help/pubs/pil/image-tk.html。 (2认同)