Edit: I've narrowed down the problem to the fact that the code inside the function is not correctly referencing the main canvas widget, but because the function is called via a bind event, I'm not quite sure how to reference the main canvas...(more detail at the bottom)
I am have the following code that displays an image inside a tkinter canvas widget. This is the code for the container creation:
canvas = tk.Canvas(width=640, height=480)
canvas.place(x=50, y=50)
canvas.bind("<B1-Motion>", draw_rectangle)
test_img = ImageTk.PhotoImage(Image.open("test_image.png"))
image_container = canvas.create_image(0, 0, anchor="nw", image=test_img)
This works exactly as expected: the image displays inside the canvas. The next step is to update the image displayed using an opencv image that has a rectangle added, converted to a TkImage, and then handed to the canvas as follows (all of the tutorials I've found use some version of this same methodology). This was done inside a function:
def draw_rectangle(event):
cv2img = cv2.rectangle(data, pt1=(x1, y1), pt2=(x2, y2), color=(255, 255, 255), thickness=5)
img = Image.fromarray(cv2img)
photo_img = ImageTk.PhotoImage(img)
canvas.itemconfig(image_container, image=photo_img)
The cv2img displays correctly in it's own window if I call:
cv2.imshow("image", cv2img)
...and the Image.fromarray shows correctly in it's own window if I call:
img.show()
Yet when I try to pass the image to the canvas, I just end up with a blank canvas. There are no errors displayed in the console. Any ideas?
P.S. I know that the color channels will be reversed but I haven't swapped them yet as it seemed less of a priority than getting an image to display :)
UPDATE
I neglected a very important piece of the puzzle in my original question. The code to update the canvas is in a function that is called when the mouse button is moved through a binding.
I moved the updated code outside of the function, and it works as expected. But this leads me to a related problem...
Becuase the function is called via an event binding (mouse move) I'm suspect that the "canvas" call inside the function is not pointing towards the canvas created outside the function... but I'm not quite sure how to correct that as the first positional argument needs to the be the event...
I've updated the code above.