2016-07-13 78 views
-1

我想更改數據,只是將某些像素切換爲白色或黑色,但我沒有得到它的工作,我可以更改數據?當我使用行np.roll()時,它會滾動圖像並更新它,但是當我使用任何其他方法簡單地更改數據數組的值時,我失敗了?Tkinter更改數據

我該怎麼做,我錯了什麼?

import Tkinter 
from PIL import Image, ImageTk 
import numpy 
import time 

times=1 
timestart=time.clock() 
data=numpy.array(numpy.random.random((68,68))*100,dtype=int) 
#data = numpy.zeros([68,68]) 
photo = None 
sw = 1 

def image_loop(): 
     global data 
     global times 
     global timestart 
     global photo 

     im=Image.fromstring('L', (data.shape[1],data.shape[0]), data.astype('b').tostring()) 
     photo = ImageTk.PhotoImage(image=im) 
     canvas.create_image(0,0,image=photo,anchor=Tkinter.NW) 
     root.update() 
     times+=1 
     if times%33==0: 
       print "%.02f FPS"%(times/(time.clock()-timestart)) 

     root.after(100,image_loop) 
     ### THE next line WORKS, why not the others ??????? 
     #data=numpy.roll(data,-1,1) 


     ### Does not work ?? 
     global sw 
     if sw == 0: 
       for i in range(68): 
         data[10][i] * 100 
         #data = numpy.array(numpy.zeros(68,68)) 
       sw = 1 
       print "hi" 
     else: 
       sw = 0 
       for i in range(68): 
         data[10][i]/100 
         data = numpy.array(numpy.ones(68,68)) 
         #data = data * 100 
       print "ho" 



root = Tkinter.Tk() 
frame = Tkinter.Frame(root, width=68, height=68) 
frame.pack() 
canvas = Tkinter.Canvas(frame, width=68,height=68) 
canvas.place(x=-2,y=-2) 

root.after(0,image_loop) # INCREASE THE 0 TO SLOW IT DOWN 
#root.mainloop() 

while True: 
     print "do work again and again, change data" 
     root.update() 
     root.update_idletasks() 


     print data.shape 
+0

爲什麼我在這裏投票? –

回答

2

事情是在初始化時不隨機地初始化數據變量。此代碼適用於95幀每秒

import Tkinter 
from PIL import Image, ImageTk 
import numpy 
import time 

times=1 
timestart=time.clock() 
data = None 
photo = None 

sw = 1 

def image_loop(): 
     global data 
     global times 
     global timestart 
     global photo 

     im=Image.fromstring('L', (data.shape[1],data.shape[0]), data.astype('b').tostring()) 
     photo = ImageTk.PhotoImage(image=im) 
     canvas.create_image(0,0,image=photo,anchor=Tkinter.NW) 
     root.update() 
     times+=1 
     if times%33==0: 
       print "%.02f FPS"%(times/(time.clock()-timestart)) 

     root.after(10,image_loop) 



root = Tkinter.Tk() 
frame = Tkinter.Frame(root, width=68, height=68) 
frame.pack() 
canvas = Tkinter.Canvas(frame, width=68,height=68) 
canvas.place(x=-2,y=-2) 

root.after(0,image_loop) 

data=numpy.array(numpy.random.random((68,68))*100,dtype=int) 


while True: 
     #print "do work again and again, change data" 
     root.update() 
     root.update_idletasks() 

     global sw 
     if sw == 0: 
       for i in range(68): 
         data[10][i] = 250 
       sw = 1 
     else: 
       sw = 0 
       for i in range(68): 
         data[10][i] = 0 
"plot.py" 58L, 1190C                                          1,1   Top 
+0

而不是在循環中調用'create_image',你可能不會重新配置一個現有的畫布圖像對象。正如你所寫的,你有一點內存泄漏,因爲你在畫布上每秒創建100個圖像對象,並且永遠不會破壞舊圖像對象。創建幾千張圖像後,畫布可能會出現性能問題。否則,這是一個很好的解決方案! –

+0

嗨@BryanOakley你是什麼意思,用「你不能重新配置現有的畫布」?我不創建100個圖像,它是一個68x68的數組,並且數組的值與100相乘,以在圖像中看到像素爲白色。現在我用超過1億張圖片運行此代碼,並且沒有增加內存。我不摧毀舊的,我簡單地覆蓋它 –

+0

唉!這是一個錯字。我的意思是說「你**可能**只是重新配置......」。我想我開始寫「爲什麼你不能......」,然後改變它。 Mea culpa。內存泄漏會很小,但它存在。垃圾收集器處理圖像的內存ok,但每次調用'create_image'時,底層的tcl/tk代碼都會創建一個永不會被破壞的新畫布對象。 –