2016-09-17 50 views
0

我正在寫一個Gtk程序,用於處理圖像。我有菜單的應用程序窗口,並將其中一個按鈕連接到Gtk.FileChooser獲取文件名(我可以用Gtk.Image()打開它,但不能用這種對象afaik做很多事情)。問題是我不知道如何將文件名傳遞給我的init函數,所以我可以使用opencv從該文件名打開圖像(需要能夠在該圖像上使用鼠標進行繪製,這就是爲什麼opencv3)。下面是使用代碼結構林:Python 3:將文件名變量從Gtk.FileChooser傳遞給__init__函數

class main_win(Gtk.Window): 
def __init__(self): 
'''menu stuff and box with widgets, few labels''' 

    def FileChooser(self): 
    dialog = Gtk.FileChooserDialog("Open a File Image", self, Gtk.FileChooserAction.OPEN, 
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, 
            Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) 


    response = dialog.run() 
    if response == Gtk.ResponseType.OK: 
     path = dialog.get_filename() 
    elif response == Gtk.ResponseType.CANCEL: 
     pass 
    dialog.destroy() 

理想我想prefere放在這裏面的init:

img = cv2.imread(path,0) 
cv2.imshow('image',img) 

然後據我所知,我可以在OpenCV的窗口做的東西與圖像例如像越來越像素(從用鼠標繪製的區域或通過鼠標點擊一個像素)統計到gtk.label或繪圖中。

即時通訊相當新的python,所以也許我問......超級簡單或超級愚蠢; p 預先感謝;)。

回答

0

那麼,也許你有一個合法的理由使用OpenCV來簡單地顯示圖像(因爲例如你在你的代碼中的其他地方使用該庫),但你可以使用GTK + 3的設施。

import os, re 

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 

class main_win(Gtk.Window): 

    def __init__(self): 
     '''menu stuff and box with widgets, few labels''' 

     super().__init__(title="Image show") 

     main_column = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL) 
     self.add(main_column) 

     button1 = Gtk.Button(label="Get Image") 
     button1.connect("clicked", self.FileChooser) 
     main_column.pack_start(button1, True, True, 0) 

     self.embedded_image = Gtk.Image() 
     main_column.pack_start(self.embedded_image, True, True, 0) 


    def FileChooser(self, widget): 
     dialog = Gtk.FileChooserDialog("Open a File Image", self, Gtk.FileChooserAction.OPEN, 
             (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, 
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) 


     response = dialog.run() 
     if response == Gtk.ResponseType.OK: 
      path = dialog.get_filename() 
      self.embedded_image.set_from_file(path) 
      self.show_all() 

     dialog.destroy() 

main_windows = main_win() 
main_windows.connect("delete-event", Gtk.main_quit) 
main_windows.show_all() 
Gtk.main()  

您可以使用Gtk.Image()在您的界面的任何位置放置圖像。您可以使用.set_from_file()進行更新。

+0

這就是我在第一時間做了,但我怎麼也找不到得出某種人物選擇一個區域用鼠標將像素移至opencv。我也寫了幾行文件對話框函數,創建一個帶有路徑的txt文件,並用其他函數打開它。仍然看起來,即使它工作,我得到一個ocv圖像窗口,我不能做任何事情在gtk.mainwindow像點擊另一個按鈕,直到我用鼠標移動gtkwindow或關閉ocv之一。 ; [ 因爲我不知道什麼模塊或者gtk3 +函數用於在打開的圖像上用鼠標執行操作 – lesti

0

遷PyQt4的,不僅OpenCV的,但也matplotlib在那裏工作順利;)