Oct-17-2016, 12:00 PM
I started with this post and have tried to convert it to python3 - where the tkinter special in pilow has been 'replaced' by 'equivalent' functionality in tkinter.
After much searching and testing I have eventually resorted to writing a file and reading it into the tkinter photoimage object, which is really naff, but is the only way I have found to make it work.
Surely there is away to pass the data direct to photoimage?
Interestingly my write / read a file approach is still faster than the original version in python 2.7!
Here is my python 3 version of the code:
After much searching and testing I have eventually resorted to writing a file and reading it into the tkinter photoimage object, which is really naff, but is the only way I have found to make it work.
Surely there is away to pass the data direct to photoimage?
Interestingly my write / read a file approach is still faster than the original version in python 2.7!
Here is my python 3 version of the code:
import tkinter
from PIL import Image
import numpy
import time
import io
#python2 version (original) -> 120fps
#full physical file io and new image each cycle -> 130fps
#reuse PIL Image instead of create new each time -> 160fps
class mainWindow():
times=1
timestart=time.clock()
data=numpy.array(numpy.random.random((400,500))*100,dtype=int)
theimage = Image.frombytes('L', (data.shape[1],data.shape[0]), data.astype('b').tostring())
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)
self.root.after(0,self.start) # INCREASE THE 0 TO SLOW IT DOWN
self.root.mainloop()
def start(self):
global data
global theimage
self.theimage.frombytes(self.data.astype('b').tobytes())
self.theimage.save('work.pgm')
self.photo = tkinter.PhotoImage(file='work.pgm')
self.canvas.create_image(0,0,image=self.photo,anchor=tkinter.NW)
self.root.update()
self.times+=1
if self.times%33==0:
print("%.02f FPS"%(self.times/(time.clock()-self.timestart)))
self.root.after(10,self.start)
self.data=numpy.roll(self.data,-1,1)
if __name__ == '__main__':
x=mainWindow()
