简体   繁体   English

如何在图像 Tkinter 画布上绘图,PIL

[英]How to draw on an image Tkinter canvas, PIL

I would want to be able to draw on an image being displayed by a tkinter canvas PIL.我希望能够在 tkinter 画布 PIL 显示的图像上绘图。 I tried google searching but all the results would be how to draw on an image without tkinter or how to display an image which is not what i want.我尝试了谷歌搜索,但所有结果都是如何在没有 tkinter 的情况下绘制图像或如何显示不是我想要的图像。 I also would like it to be like live(color while i move my mouse).我也希望它像现场一样(移动鼠标时的颜色)。 Is there any way to do this.有没有办法做到这一点。 If there are no ways with PIL is there a way with tkinter.如果 PIL 没有办法,那么 tkinter 是否有办法。

Answer回答

the answer from this post here if the link wont work here is the code:如果链接在此处不起作用,则此帖子的答案是代码:

from tkinter import *
from tkinter import ttk

class Sketchpad(Canvas):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self.bind("<Button-1>", self.save_posn)
        self.bind("<B1-Motion>", self.add_line)
        
    def save_posn(self, event):
        self.lastx, self.lasty = event.x, event.y

    def add_line(self, event):
        self.create_line((self.lastx, self.lasty, event.x, event.y), width=20, capstyle='round')
        self.save_posn(event)

root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

sketch = Sketchpad(root)
sketch.grid(column=0, row=0, sticky=(N, W, E, S))

root.mainloop()

I think the best approach would be to do the drawing with tkinter because otherwise you will need to continually update the canvas widget every time the image gets changed by the PIL (which might be too slow and interfere with "live" drawing).我认为最好的方法是使用tkinter进行绘图,因为否则每次 PIL 更改图像时,您都需要不断更新画布小部件(这可能太慢并干扰“实时”绘图)。

tkinter Canvas es have several different vector and text drawing capabilities, and you can draw/display them on top of an image that's also being displayed by it. tkinter Canvas es 具有几种不同的矢量和文本绘图功能,您可以在也由它显示的图像之上绘制/显示它们。 This means if you first add an image, anything further that is drawn will appear on top of that (if it overlaps the image).这意味着如果您首先添加图像,则进一步绘制的任何内容都会出现在该图像之上(如果它与图像重叠)。

Below is a demonstration of doing that.下面是这样做的演示。 Note I borrowed some of the code and the sample image from the python-course.eu page: Canvas Widgets in Tkinter .注意我从python-course.eu页面借用了一些代码和示例图像: Canvas Widgets in Tkinter It describes the drawing facilities supplied by Canvas widgets.它描述了Canvas小部件提供的绘图工具。

from PIL import Image, ImageTk
import tkinter as tk
from tkinter.constants import *

canvas_width, canvas_height = 300, 300

root = tk.Tk()

canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()

img = ImageTk.PhotoImage(file='rocks.png')
canvas.create_image(20, 20, anchor=NW, image=img)

# Draw something on top of image.
points = [100, 140, 110, 110, 140, 100, 110, 90, 100, 60, 90, 90, 60, 100, 90, 110]
canvas.create_polygon(points, outline='green', fill='yellow', width=3)

root.mainloop()

Here's the rocks.png image the code uses.这是代码使用的rocks.png图像。

Screenshot of result:结果截图:

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM