我有两个大小完全相同的图像。一个是基础背景图像,另一个是背景图像的更改版本。我正在尝试在背景图像的顶部显示更改后的图像的静态大小(例如100,100)的窗口,这些窗口在鼠标移动到的任何地方都对齐。一种移动窗口,将更改后的图像覆盖在基本图像上。我一直在尝试修改一些我在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()


背景图片:
python - tkinter-用鼠标移动在 Canvas 图像上显示图像覆盖-LMLPHP

变更的图片:
python - tkinter-用鼠标移动在 Canvas 图像上显示图像覆盖-LMLPHP

所需结果:
python - tkinter-用鼠标移动在 Canvas 图像上显示图像覆盖-LMLPHP

最佳答案

实际上,您几乎完成了所有操作。您只需要重新放置一些内容并在其中添加一些内容(我不会以干净的方式进行操作,否则我将不得不更改更多内容。稍后您可以自己进行操作,以确保其中没有重复的行您的代码):


更改代码的这一部分:

# 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>",以便在您按住“左键”并移动鼠标时运行。
更改此:

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")



大功告成!正如您所看到的,只是您自己的所有代码随处可见,并且带有一些香料。我没有在画布上做过很多工作,所以我不知道最后一部分是否会继续在其他部分之上创建新图像或替换已经存在的图像。所以我不知道这是否是最好的代码,但是它可以工作。
您最好清除此代码,因为您可以看到move_window中的许多代码与mainloop之前的代码相同
祝好运。

关于python - tkinter-用鼠标移动在 Canvas 图像上显示图像覆盖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45762991/

10-15 00:44
查看更多