2012-07-29 34 views
0

這裏的問題 -像素處理

我正在寫一個程序,將通過一系列的圖像操作迭代 - 既影響整體形象,以及只是部分圖片。我需要向用戶(我自己)演示這些更改,以便我可以在發生操作時看到發生了什麼問題。

我本來試圖用PIL和Tkinter的做到這一點 - 但我不能圖像甚至加載到GUI - 這裏有一個位從網絡的角落形成代碼,通過衆多的谷歌搜索:

from Tkinter import * 
import Image, ImageDraw, ImageTk 
import time 

class Test(Frame): 
    def __init__(self): 
     Frame.__init__(self) 
     self.c = Canvas(self, width=574, height=431, bg="red") 
     self.c.pack() 
     Button(self, text="Process", command=self.procImg).pack() 
     Button(self, text="Quit", command=self.quit).pack() 
    def procImg(self): 
     t = time.time() 
     self.flashImg = Image.open("./in_img/resize.bmp") 
     #self.flashImg = flashImg.resize((574, 431)) 
     self.flashImg = self.flashImg.convert("L") 
     #self.photo = ImageTk.BitmapImage(flashImg) 
     self.c.photo = ImageTk.PhotoImage(self.flashImg) 
     self.c.create_image(574, 431, anchor=NW, image=self.c.photo) 
     self.c.create_rectangle(50, 50, 100, 100, fill="blue") 
     self.update() 
     print time.time()-t 

t = Test() 
t.pack() 
t.mainloop() 

所以上面的代碼非常糟糕,我知道 - 但我想發佈一些東西來證明我至少在這方面工作。

任何人都可以向我推薦一種使用Python來解決這個問題的新方法嗎?我寧願不學習不同的語言 - 我是Tkinter圖書館的新手,所以如果其他方面更適合這一點,我沒有任何問題學習一個新的圖書館。另外,僅供參考,「resize.bmp」圖片是來自數碼相機的調整大小的.JPG。我也嘗試過這樣一種方法,但它不起作用 - 我真的需要找到一種方法來在內存中將位圖從閃存中刷到屏幕上,以便在處理過程中可以調整參數。

感謝您的幫助!

回答

1

圖像可能在那裏。它只是不可見。

相反的:

self.c.create_image(574, 431, anchor=NW, image=self.c.photo) 

嘗試:

self.c.create_image(0, 0, anchor=NW, image=self.c.photo) 

另外,如果你堅持到畫布圖像項目的引用,你可以換不同的圖像和縮小。

例如。 (self.canvasItem)如下:

from Tkinter import * 
from PIL import Image, ImageTk, ImageDraw, ImageOps, ImageEnhance 


class ImageButcher(Tk): 
    def __init__(self): 
     Tk.__init__(self) 

     #create ui 
     f = Frame(self, bd=2) 

     self.colour = StringVar(self) 
     self.colourMenu = OptionMenu(f, self.colour, 
            *('red','green','blue','white')) 
     self.colourMenu.config(width=5) 
     self.colour.set('red') 
     self.colourMenu.pack(side='left') 

     self.rectangleButton = Button(f, text='Rectangle', 
            command=self.draw_rectangle) 
     self.rectangleButton.pack(side='left') 

     self.brightenButton = Button(f, text='Brighten', 
            command=self.on_brighten) 
     self.brightenButton.pack(side='left') 

     self.mirrorButton = Button(f, text='Mirror', 
            command=self.on_mirror) 
     self.mirrorButton.pack(side='left') 
     f.pack(fill='x') 

     self.c = Canvas(self, bd=0, highlightthickness=0, 
         width=100, height=100) 
     self.c.pack(fill='both', expand=1) 

     #load image 
     im = Image.open('IMG_1584.JPG') 
     im.thumbnail((512,512)) 

     self.tkphoto = ImageTk.PhotoImage(im) 
     self.canvasItem = self.c.create_image(0,0,anchor='nw',image=self.tkphoto) 
     self.c.config(width=im.size[0], height=im.size[1]) 

     self.img = im 
     self.temp = im.copy() # 'working' image 

    def display_image(self, aImage): 
     self.tkphoto = pic = ImageTk.PhotoImage(aImage) 
     self.c.itemconfigure(self.canvasItem, image=pic) 

    def on_mirror(self): 
     im = ImageOps.mirror(self.temp) 
     self.display_image(im) 
     self.temp = im 

    def on_brighten(self): 
     brightener = ImageEnhance.Brightness(self.temp) 
     self.temp = brightener.enhance(1.1) # +10% 
     self.display_image(self.temp) 

    def draw_rectangle(self): 
     bbox = 9, 9, self.temp.size[0] - 11, self.temp.size[1] - 11   
     draw = ImageDraw.Draw(self.temp) 
     draw.rectangle(bbox, outline=self.colour.get()) 
     self.display_image(self.temp) 


app = ImageButcher() 
app.mainloop() 
+0

Dude that works。史詩 - 這回答我的問題。謝謝! – jimf 2012-08-12 02:21:14