python - tkinter - show image overlay on a canvas image with mouse move -
i have 2 images exact same size. 1 base background image , other image altered version of background image. trying show window of static size (such 100,100) of altered image on top of background image line wherever mouse moved. sort of moving window overlaying altered image on base image. have been trying modify base code found somewhere here on stack overflow, can't seem figure out how modify desired result. please suggest alternative method if think going wrong way, or if there close example somewhere have missed. new tkinter. also, have no need coordinate system shown in image examples below. image alone good.
from tkinter import * pil import imagetk, image scipy.misc import imread event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y)) # function called when mouse moved def move_window(event): # outputting x , y coords console cx, cy = event2canvas(event, canvas) print("(%d, %d) / (%d, %d)" % (event.x, event.y, cx, cy)) if __name__ == "__main__": root = tk() #setting tkinter canvas 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 , altered image numpy array. background_image_data = imread("images/original.png", mode="l") foreground_image_data = imread("images/altered.png", mode="l") # here mouse coordinates should injected move window on 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()
you did of it. need reposition of things , add few things here , there (i'm not going in clean way otherwise have change more stuff. can later not have duplicate lines in code):
change part of code:
# loading background , altered image numpy array. background_image_data = imread("images/original.png", mode="l") foreground_image_data = imread("images/altered.png", mode="l") # here mouse coordinates should injected move window on 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())to this:
# loading background , altered image numpy array. background_image_data = imread("images/original.png", mode="l") foreground_image_data = imread("images/altered.png", mode="l") '''shouldn't change original background image because way changes permanent''' bg_img = background_image_data.copy() # here mouse coordinates should injected move window on 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())change
"<buttonpress-1>""<motion>"run change of mouse position. or change"<b1-motion>"runs whenever holding down left-button , moving mouse.change this:
def move_window(event): cx, cy = event2canvas(event, canvas)to this:
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")
and you're done! can see it's own code moved around, , few spices. haven't worked canvas don't know if last part keep creating new image on top of other or replacing existing one. don't know if it's best code use there, works.
better clean code because can see lot of code move_window same before mainloop
luck.



Comments
Post a Comment