如何使用tkinter在图像上绘制坐标系?

2024-06-02 06:17:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想显示一个图像,并在上面画一个坐标系(x轴,y轴)。 我可以展示图像

img = ImageTk.PhotoImage(Image.open(path).resize((400,400),Image.ANTIALIAS))
panel = tk.Label(root,image = img, width = 400, height = 400s)
panel.pack(size= "bottom", fill = "both", expand = "yes")
panel.place(rely = 073, rely = 0.5s) 

我还可以为x轴/y轴画一条线(如果你知道一种方法来画一个闪光而不是一条线,那就更好了)

^{pr2}$

我不能做的是把这条线放在图像的顶部。我试着用画布.地点(),但当我把它放在图像的顶部时,我再也看不到图像了。有没有办法让画布透明?或者我还能做点什么吗?我是新手。在

谢谢。在

编辑:显然不可能使画布透明Transparent canvas in tkinter python

但是我可以在画布上添加一个背景图像,然后画一条线,这样我就可以看到两者了吗?在


Tags: path图像imageimg画布openlabeltk
1条回答
网友
1楼 · 发布于 2024-06-02 06:17:45

您必须将图像放置在画布上,然后在其顶部放置轴线:

import Tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()

img_path = 'water-drop-splash.png'
img = ImageTk.PhotoImage(Image.open(img_path).resize((400,400), Image.ANTIALIAS))

canvas = tk.Canvas(root, height=400, width=400)
canvas.create_image(200, 200, image=img)
canvas.create_line(0, 200, 399, 200, dash=(2,2))  # x-axis
canvas.create_line(200, 0, 200, 399, dash=(2,2))  # y-axis
canvas.pack()

root.mainloop()

塔达!在

screenshot of tkinter window displaying image and axes

相关问题 更多 >