Here is a working solution, slightly modified to make it work (some function is deprecated) and simplify it to save only the necessary part. We should use Image.frombytes(...) to read the data in the numpy matrix.
import Tkinter from PIL import Image, ImageTk import numpy class mainWindow(): def __init__(self): self.root = Tkinter.Tk() self.frame = Tkinter.Frame(self.root, width=500, height=400) self.frame.pack() self.canvas = Tkinter.Canvas(self.frame, width=500,height=400) self.canvas.place(x=-2,y=-2) data=numpy.array(numpy.random.random((400,500))*100,dtype=int) self.im=Image.frombytes('L', (data.shape[1],data.shape[0]), data.astype('b').tostring()) self.photo = ImageTk.PhotoImage(image=self.im) self.canvas.create_image(0,0,image=self.photo,anchor=Tkinter.NW) self.root.update() self.root.mainloop() mainWindow()
source share