tkinter supports only a few image formats directly, but one of them, PPM can be created easily from numpy data. So, here is a solution which converts an array directly to a tk.PhotoImage - no need to take the detour (and overhead!) of ImageTk:
import tkinter as tk
import numpy as np
def _photo_image(image: np.ndarray):
height, width = image.shape
data = f'P5 {width} {height} 255 '.encode() + image.astype(np.uint8).tobytes()
return tk.PhotoImage(width=width, height=height, data=data, format='PPM')
root = tk.Tk()
array = np.ones((40, 40)) * 150
img = _photo_image(array)
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
canvas.create_image(20, 20, anchor="nw", image=img)
root.mainloop()
The magic is in the function _photo_image which creates a portable pixmap header and appends the picture data, which must be an array of bytes.
Notes:
the above creates a portable graymap (PGM). With a slight modification, this also works with color images. These have one more dimension. So, use
height, width = image.shape[:2]
to extract the geometry and P6 for the magic value to pass in the header.
For example, to convert an openCV image (which is usually encoded as BGR), use:
import cv2
def _photo_image(image: np.ndarray):
height, width = image.shape[:2]
ppm_header = f'P6 {width} {height} 255 '.encode()
data = ppm_header + cv2.cvtColor(image, cv2.COLOR_BGR2RGB).tobytes()
return tk.PhotoImage(width=width, height=height, data=data, format='PPM')
the above link to the English Wikipedia page on netpbm does not fully explain the header format (you can find it in the examples, though). The German Wikipedia page on portable anymap has more details on the header: Magic Value, space, width, space, height, space, max-pixel-value, space
*but usingtk.. Should probably fix that first.