tkinter - 通过鼠标移动在 canvas 图像上显示图像叠加层
tkinter - show image overlay on a canvas image with mouse move
我有两张大小完全相同的图片。一个是基本背景图像,另一个图像是背景图像的更改版本。我试图在背景图像的顶部显示 window 的静态大小(例如 100,100)的已更改图像,无论鼠标移动到哪里,它们都排成一行。有点像移动 window 将更改后的图像覆盖在基本图像上。我一直在尝试修改我在 Stack Overflow 上某处找到的一些基本代码,但似乎无法弄清楚如何修改它以获得所需的结果。如果您认为我以错误的方式解决这个问题,或者如果我错过了某个地方有一个接近的例子,请建议一种替代方法。我对 tkinter 很陌生。另外,我不需要下面图像示例中显示的坐标系。光是图就不错
from tkinter import *
from PIL import ImageTk, Image
from scipy.misc import imread
event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y))
# function to be called when mouse is moved
def move_window(event):
# outputting x and y coords to console
cx, cy = event2canvas(event, canvas)
print("(%d, %d) / (%d, %d)" % (event.x, event.y, cx, cy))
if __name__ == "__main__":
root = Tk()
#setting up a tkinter canvas with scrollbars
frame = Frame(root, bd=2, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = Scrollbar(frame, orient=HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=E+W)
yscroll = Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=BOTH,expand=1)
# loading background and altered image into numpy array.
background_image_data = imread("images/original.png", mode="L")
foreground_image_data = imread("images/altered.png", mode="L")
# Here is where the mouse coordinates should be injected to move the window over the background image
window_data = foreground_image_data[500:600, 500:600]
background_image_data[500:600, 500:600] = window_data
img = ImageTk.PhotoImage(Image.fromarray(background_image_data))
canvas.create_image(0, 0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
test = canvas.bind("<ButtonPress-1>", move_window)
root.mainloop()
背景图片:
改变图像:
期望的结果:
您实际上几乎完成了所有工作。你只需要重新定位一些东西并在这里和那里添加一些东西(我不会以干净的方式做,否则我必须改变更多的东西。你可以稍后自己做,以避免重复行你的代码):
更改这部分代码:
# loading background and altered image into numpy array.
background_image_data = imread("images/original.png", mode="L")
foreground_image_data = imread("images/altered.png", mode="L")
# Here is where the mouse coordinates should be injected to move the window over the background image
window_data = foreground_image_data[500:600, 500:600]
background_image_data[500:600, 500:600] = window_data
img = ImageTk.PhotoImage(Image.fromarray(background_image_data))
canvas.create_image(0, 0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
对此:
# loading background and altered image into numpy array.
background_image_data = imread("images/original.png", mode="L")
foreground_image_data = imread("images/altered.png", mode="L")
'''Shouldn't change the original background image because
that way the changes will be permanent'''
bg_img = background_image_data.copy()
# Here is where the mouse coordinates should be injected to move the window over the background image
x,y,wh = (50,50,100)
window_data = foreground_image_data[y:y+wh,x:x+wh]
bg_img[y:y+wh,x:x+wh] = window_data
img = ImageTk.PhotoImage(Image.fromarray(bg_img))
canvas.create_image(0, 0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
将 "<ButtonPress-1>"
更改为 "<Motion>"
以便通过更改鼠标位置来 运行。或者将其更改为 "<B1-Motion>"
,这样只要您按住左键并移动鼠标,它就会 运行s。
改变这个:
def move_window(event):
cx, cy = event2canvas(event, canvas)
为此:
def move_window(event):
global img
cx, cy = event2canvas(event, canvas)
x,y,wh = (int(cx),int(cy),100)
window_data = foreground_image_data[y:y+wh,x:x+wh]
bg_img = background_image_data.copy()
bg_img[y:y+wh,x:x+wh] = window_data
img = ImageTk.PhotoImage(Image.fromarray(bg_img))
canvas.create_image(0, 0,image=img,anchor="nw")
大功告成!如您所见,所有您自己的代码都只是四处移动,并添加了一些香料。我没有经常使用 canvas,所以我不知道最后一部分是否会继续在另一个图像之上创建新图像或替换现有图像。所以我不知道它是否是在那里使用的最佳代码,但它确实有效。
您最好清理此代码,因为您可以看到 move_window
中的很多代码与 mainloop
之前的代码相同
祝你好运。
我有两张大小完全相同的图片。一个是基本背景图像,另一个图像是背景图像的更改版本。我试图在背景图像的顶部显示 window 的静态大小(例如 100,100)的已更改图像,无论鼠标移动到哪里,它们都排成一行。有点像移动 window 将更改后的图像覆盖在基本图像上。我一直在尝试修改我在 Stack Overflow 上某处找到的一些基本代码,但似乎无法弄清楚如何修改它以获得所需的结果。如果您认为我以错误的方式解决这个问题,或者如果我错过了某个地方有一个接近的例子,请建议一种替代方法。我对 tkinter 很陌生。另外,我不需要下面图像示例中显示的坐标系。光是图就不错
from tkinter import *
from PIL import ImageTk, Image
from scipy.misc import imread
event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y))
# function to be called when mouse is moved
def move_window(event):
# outputting x and y coords to console
cx, cy = event2canvas(event, canvas)
print("(%d, %d) / (%d, %d)" % (event.x, event.y, cx, cy))
if __name__ == "__main__":
root = Tk()
#setting up a tkinter canvas with scrollbars
frame = Frame(root, bd=2, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = Scrollbar(frame, orient=HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=E+W)
yscroll = Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=BOTH,expand=1)
# loading background and altered image into numpy array.
background_image_data = imread("images/original.png", mode="L")
foreground_image_data = imread("images/altered.png", mode="L")
# Here is where the mouse coordinates should be injected to move the window over the background image
window_data = foreground_image_data[500:600, 500:600]
background_image_data[500:600, 500:600] = window_data
img = ImageTk.PhotoImage(Image.fromarray(background_image_data))
canvas.create_image(0, 0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
test = canvas.bind("<ButtonPress-1>", move_window)
root.mainloop()
背景图片:
改变图像:
期望的结果:
您实际上几乎完成了所有工作。你只需要重新定位一些东西并在这里和那里添加一些东西(我不会以干净的方式做,否则我必须改变更多的东西。你可以稍后自己做,以避免重复行你的代码):
更改这部分代码:
# loading background and altered image into numpy array. background_image_data = imread("images/original.png", mode="L") foreground_image_data = imread("images/altered.png", mode="L") # Here is where the mouse coordinates should be injected to move the window over the background image window_data = foreground_image_data[500:600, 500:600] background_image_data[500:600, 500:600] = window_data img = ImageTk.PhotoImage(Image.fromarray(background_image_data)) canvas.create_image(0, 0,image=img,anchor="nw") canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
对此:
# loading background and altered image into numpy array. background_image_data = imread("images/original.png", mode="L") foreground_image_data = imread("images/altered.png", mode="L") '''Shouldn't change the original background image because that way the changes will be permanent''' bg_img = background_image_data.copy() # Here is where the mouse coordinates should be injected to move the window over the background image x,y,wh = (50,50,100) window_data = foreground_image_data[y:y+wh,x:x+wh] bg_img[y:y+wh,x:x+wh] = window_data img = ImageTk.PhotoImage(Image.fromarray(bg_img)) canvas.create_image(0, 0,image=img,anchor="nw") canvas.config(scrollregion=canvas.bbox(ALL), width=img.width(), height=img.height())
将
"<ButtonPress-1>"
更改为"<Motion>"
以便通过更改鼠标位置来 运行。或者将其更改为"<B1-Motion>"
,这样只要您按住左键并移动鼠标,它就会 运行s。改变这个:
def move_window(event): cx, cy = event2canvas(event, canvas)
为此:
def move_window(event): global img cx, cy = event2canvas(event, canvas) x,y,wh = (int(cx),int(cy),100) window_data = foreground_image_data[y:y+wh,x:x+wh] bg_img = background_image_data.copy() bg_img[y:y+wh,x:x+wh] = window_data img = ImageTk.PhotoImage(Image.fromarray(bg_img)) canvas.create_image(0, 0,image=img,anchor="nw")
大功告成!如您所见,所有您自己的代码都只是四处移动,并添加了一些香料。我没有经常使用 canvas,所以我不知道最后一部分是否会继续在另一个图像之上创建新图像或替换现有图像。所以我不知道它是否是在那里使用的最佳代码,但它确实有效。
您最好清理此代码,因为您可以看到 move_window
中的很多代码与 mainloop
之前的代码相同
祝你好运。