wxPython+opencv打造自己的圖片編輯軟體
GUI採用wxPython,影象處理採用opencv,製作了一款簡單的圖片處理工具。其效果如下

output.gif
主要的功能及實現
選中檔案資源管理器中的圖片
這個採用tkinter庫的filedialog模組很容易實現
image_path = filedialog.askopenfilename(initialdir=r"..\\", title="select an image whose filename endwiths .jpg/png",filetypes=[ ("JPG", ".jpg"),("PNG", ".png")])
引數一:initialdir是開啟檔案資源管理器的初始路徑,可以不設定
引數二: title是開啟的檔案資源管理器的最左上方的標題
引數三: filetypes,比如我上面的設定過濾掉了其他非 .jpg
、 .png
檔案
askopenfilename的返回值就是你選擇的檔案路徑
影象處理
我主要加了圖片塗鴉、圖片黑白化、圖片裁剪這幾個功能,具體效果參考最上面的效果圖,實現的話演算法+業務邏輯很容易
演算法算是比較簡單的演算法,但是業務邏輯和其中不少的坑是隻有動手後才能深有體會的,這裡貼出這部分的主要程式碼:
def OnImageGrayClicked(self,event): if self.isLoaded == True: dlg = wx.MessageDialog(self, u"this Operation cannot be undo", u"Warning", wx.YES_NO) value = dlg.ShowModal() if value == wx.ID_YES: self.setImage(cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)) self.editMenu.Check(ID_IMAGE_GRAY, True) self.image_gray.Enable(False) else: self.editMenu.Check(ID_IMAGE_GRAY, False) dlg.Destroy() else: wx.MessageDialog(self, u"please make sure you have loaded the image successfully", u"Warning", wx.OK).ShowModal() self.editMenu.Check(ID_IMAGE_GRAY, False) def OnImageDrawClicked(self,event): if self.isLoaded == True: self.canDraw = not self.canDraw if self.canDraw == True: self.image_draw.SetItemLabel("disabel drawing on the image") self.image_cut.Enable(False) else: self.image_draw.SetItemLabel("enabel drawing on the image") self.image_cut.Enable(True) pass else: wx.MessageDialog(self, u"please make sure you have loaded the image successfully", u"Warning", wx.OK).ShowModal() def OnImageCutClicked(self,event): if self.isLoaded == True: self.canCut = not self.canCut if self.canCut == True: self.image_copy = self.image.copy() self.image_cut.SetItemLabel("disabel cutting on the image") self.image_draw.Enable(False) else: self.image_cut.SetItemLabel("enabel cutting on the image") self.image_draw.Enable(True) return else: wx.MessageDialog(self, u"please make sure you have loaded the image successfully", u"Warning", wx.OK).ShowModal() def OnSettingsLineWeightClicked(self,event): self.lineWeight = wx.GetNumberFromUser(message="input the line_weight(from 1 to 10)", prompt="lineWeight", caption="drawer settings", value=self.lineWeight, parent=self.bmp, max=10, min=1) print("SLW") pass
去掉opencv自帶的視窗,把圖片繫結到wxPython框架上來
我感覺這部分是最複雜的,需要兩個例項,image = cv2.imread()和bitmap = wx.bitmap(),從而搭起一個從opencv到wxPython的橋樑
這裡最核心的程式碼是
def setImage(self,img): self.image = img img_height, img_width = img.shape[:2] try: image1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) except: image1 = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB) finally: pic = wx.Bitmap.FromBuffer(img_width, img_height, image1) # 顯示圖片在panel上 self.bmp.SetBitmap(pic)
關於程式碼
程式碼設計的初衷是做一款功能比較全面的圖片編輯軟體,但是一個人的力量有限,我寫了一個晚上,只寫好了框架和 圖片裁剪
、 圖片黑白化
、 圖片塗鴉
這幾個功能,希望以後有小夥伴一起參與進來
連結: ofollow,noindex">https://pan.baidu.com/s/1heSY9U-DJnRinoqihD-eJA
提取碼:5k63
(小聲bb,過了期末考試周就發程式碼,程式碼協作也是)