I want to create image with numpy (just create array that represents image), then I want to draw something on it using open-cv, and then show that image on a tkinter Canvas widget (with help from Pillow Image and ImageTk). The thing is that if I execute this code, nothing shows up in Canvas widget.
import numpy as np
import cv2
from PIL import Image, ImageTk
from tkinter import *
frame = np.full((150, 150, 3), 240, dtype="uint8")
cv2.circle(frame, (75, 75), 50, [0, 0, 0], -1, cv2.LINE_AA)
img = Image.fromarray(frame)
img2 = ImageTk.PhotoImage(image=img)
btns[i][j].create_image(75, 75, anchor="center", image=img2) # btns[i][j] is Canvas object
But if I execute this code:
import numpy as np
import cv2
from PIL import Image, ImageTk
from tkinter import *
frame = np.full((150, 150, 3), 240, dtype="uint8")
cv2.circle(frame, (75, 75), 50, [0, 0, 0], -1, cv2.LINE_AA)
img = Image.fromarray(frame)
img.show()
img2 = ImageTk.PhotoImage(image=img)
btns[i][j].create_image(75, 75, anchor="center", image=img2) # btns[i][j] is Canvas object
(after closing popup window showing the image, which looks fine) it shows correctly on Canvas. The only difference was that I used show() on Pillow Image. I can't understand why would I need to show Image first for it to work in tkinter.