wxPython是用python實現界面的東西,本文由簡單圖像處理demo帶你快速上手,代碼來自我的基於深度學習的漫畫自動上色程序。模型訓練參考他人成果,在此註明鏈接:deepcolor。當然,本文不涉及深度學習模塊,只有wxPython demo。
wxPython程序結構
不唯一,以下只是我採用的寫法:
import wx
class MyFrame(wx.Frame):
def __init__(self):
# 界面窗口初始化,包含界面名字和尺寸
wx.Frame.__init__(self, None, title='這個程序的名字', size=(高, 寬))
...
def ...
...
if __name__ == '__main__':
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
main
裏面的四行中,用戶只需要關心MyFrame()
,其內容決定着這個界面的佈局和功能,剛入門的用戶也只需要專注於編寫class MyFrame
的省略號部分。
一個簡單圖像demo,只需關注MyFrame類的編寫
結合註釋理解代碼,代碼取自漫畫自動上色程序,需要兩個選擇圖片的按鈕來選擇一張線圖和一張顏色圖,還需要一個上色按鈕,點擊按鈕,完成上色。其中上色功能有coloring()
,你可以在以下代碼相應的位置更換成你自己的圖像處理程序,也可以更改按鈕的數量、更改界面的佈局等。
# -*- coding: utf-8 -*-
import wx
from useModel import * # useModel中定義了我需要的圖像上色方法,此部分你可以自行編寫
# 選圖按鈕中使用到這個,這個是過濾器,只能選擇過濾器指定格式的文件
# 由於是初學,所以我設定這個爲可選擇所有文件
filesFilter = "All files (*.*)|*.*"
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='coloring', size=(808, 400))
# 定義一個選擇圖片的按鈕,按鈕名稱、按鈕左上角的位置和按鈕的大小都需要自己設置
b1 = wx.Button(self, -1, u"選擇線圖", pos=(98, 286), size=(80, 30))
# 使用了預定義的事件綁定器對象wx.EVT_BUTTON來將按鈕b1的單擊事件
# 與方法self.OnButton1相關聯起來,下同。
# 也就是說,用戶點擊按鈕b1,將會轉去運行OnButton1這個方法(在後面定義)
self.Bind(wx.EVT_BUTTON, self.OnButton1, b1)
b2 = wx.Button(self, -1, u"選擇顏色", pos=(364,286), size=(80, 30))
self.Bind(wx.EVT_BUTTON, self.OnButton2, b2)
b3 = wx.Button(self, -1, u"開始上色", pos=(630, 286), size=(80, 30))
self.Bind(wx.EVT_BUTTON, self.OnButton3, b3)
# 點擊按鈕b1,獲得線圖的名字,並把線圖顯示在界面上
def OnButton1(self, event):
# 定義按鈕的名稱和功能,wx.FD_OPEN代表這個按鈕用於選擇、打開文件
dlg = wx.FileDialog(self, message ="選擇線圖", wildcard=filesFilter, style = wx.FD_OPEN)
# 這個if的意思暫時不必深究,總之其下定義的就是用戶點擊按鈕之後,會進行的操作
if dlg.ShowModal() == wx.ID_OK:
self.line_image_name = dlg.GetPath(); # 獲得圖片的路徑
self.line_image = cv2.imread(self.line_image_name) # 利用cv2讀圖
# 定義一個顯示圖片的控件
self.PicShow1 = wx.StaticBitmap(self, -1, wx.NullBitmap, pos=(10, 20), size=(256, 256))
# 定義圖片,並在圖中顯示圖片
img = wx.Image(self.line_image_name, wx.BITMAP_TYPE_ANY).Scale(256, 256)
self.PicShow1.SetBitmap(wx.Bitmap(img))
dlg.Destroy()
# 點擊按鈕b2,獲得顏色圖的名字,並把顏色圖顯示在界面上
def OnButton2(self, event):
dlg = wx.FileDialog(self, message=u"選擇顏色", wildcard=filesFilter, style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.color_image_name = dlg.GetPath();
self.color_image = cv2.imread(self.color_image_name)
self.PicShow2 = wx.StaticBitmap(self, -1, wx.NullBitmap, pos=(276, 20), size=(256, 256))
img = wx.Image(self.color_image_name, wx.BITMAP_TYPE_ANY).Scale(256, 256)
self.PicShow2.SetBitmap(wx.Bitmap(img))
# 點擊按鈕b3,完成上色操作,並把結果圖顯示在界面上
def OnButton3(self, event):
self.generated_image = self.coloring(line_image_name=self.line_image_name,
color_image_name=self.color_image_name)
# 顯示上好色的圖片,目前只能做到現保存圖片,再將圖片讀入並顯示
cv2.imwrite("result/result.png", self.generated_image)
self.PicShow = wx.StaticBitmap(self, -1, wx.NullBitmap, pos=(542, 20), size=(256, 256))
img = wx.Image('result/result.png', wx.BITMAP_TYPE_ANY).Scale(256, 256)
self.PicShow.SetBitmap(wx.BitmapFromImage(img))
# 調用useModel中的useModel方法,此部分你可以更換成你自己需要的
def coloring(self, line_image_name, color_image_name):
u = useModel(line_image_name, color_image_name)
return u.get_generated_image()
if __name__ == '__main__':
app = wx.App()
frm = MyFrame()
frm.Show()
app.MainLoop()
小結一下,我是在__init__()
中定義了三個按鈕,然後定義了用戶點擊這三個按鈕對應的操作。以及上述代碼中還有我自定義的方法,讀者在實際中可以按需更改。