Python開發GUI實戰:圖片轉換素描畫工具!

今天被這句話觸動了,因此開篇分享給你們。雞湯有毒,但有時你們卻靠它激勵本身繼續前行!php

python開發GUIpython

程序員的自我救贖,使用python開發性格分析工具 這篇文章,經過Python編寫了GUI的性格分析工具。一位從我公衆號開始結識的朋友,詢問關於如何快速編寫一個exe工具的問題。因爲功能簡單且以前無相關GUI編程基礎,爲了快速完成開發,我向他推薦了easygui模塊。程序員

 

 

python做爲膠水語言,幾乎沒有不能作的事情,但我的一直以爲在GUI開發方面,python能夠算做是短板了,爲何?由於性能…python的性能問題,每每出如今其餘編程語言對其的鄙夷中。但無論如何python在GUI編程上,也是有大量優秀模塊的。web

1編程

 

tkinterapi

也許有些人不知道這個模塊,但若是你安裝python後,使用過自帶的IDLE,那麼你就應該瞭解它。tkinter做爲python自帶的GUI模塊,IDLE即是經過該模塊開發的,也是我比較喜歡的一個模塊。微信

2app

easyguidom

第一次瞭解這個模塊,仍是在小甲魚的《零基礎入門學習python》時學習的,它對Tkinter進行了二次封裝,使得GUI操做變得更爲簡單,但同時也侷限於表層的使用,沒法自定義更優雅的實現方案。koa

3

wxpython

wxpython是一個Python對於wxWidgets(這是用 C++ 編寫)包裝後的產物,算是個流行的跨平臺GUI工具包。wxpython的優點在於運行速度較快,但編寫起來較爲複雜,並且界面美化效果較差…固然有第三方開發的wxFormBuiler拖拽編輯器,能替代部分的底層代碼實現。不在意外觀但追求速度的朋友,能夠考慮它。

4

PYQT5

​Qt是一套跨平臺的C++庫,不管是C++、Java仍是Python,均可以經過它來實現GUI界面的開發。PyQt5是Qt v5的一套全面的Python綁定。它實現爲超過35個擴展模塊,上千個實現函數。使Python可以在全部支持的平臺(包括iOS和Android)上用做C++的替代開發語言。
若是你對GUI編程很是感謝興趣,能夠用心學學這個模塊,但若是你只是想快速的實現一些GUI的功能,我更推薦你們學習tkinter模塊,爲何?

  1. tkinter做爲python默認自帶的模塊庫,無需單獨安裝

  2. tkinter相比於wxpython與pyqt在製做小工具的方面,更爲高效

  3. tkinter在網上存在大量的快速入門教程

     

 

tkinter實戰

爲了證實tkinter的方便與高效,今天來編寫一個將圖片轉化爲素描畫的小工具,先給你們看看實現效果:

 

避坑指南

1

tktiner添加圖片

你們看到,tktiner中附帶了一個個人微信二維碼,這裏只是爲了演示tkinter添加圖片,沒有絲毫推廣的味道,哈哈…這裏要注意下,tktiner在添加圖片時,PhotoImage(file='tmp.gif')要求圖片必須是.gif結尾的圖片,不然就會報錯:

_tkinter.TclError: couldn’t recognize data in image file

 

2

關於base64

你們看到我沒有加載圖片,而是經過base64提早轉碼好二進制文件後,再進行導入,這樣咱們打包的exe在使用時,就無需附帶一個圖片文件了!

1import base64
2
3with open('清風Python.gif','rb') as f:
4    data = f.read()
5img = base64.b64encode(data)
6print(img)

 

 

 

 

tkinter打包

既然咱們開發的是GUI工具,必須得打包成exe才更好玩啊!可是昨天有朋友反饋,打包的地方說的太粗,光看動圖不知道該如何操做。那麼今天我簡單說下。

首先python的打包工具不少,但我一直以來比較喜歡使用pyinstaller,固然你們也能夠去嘗試cx_freeze、Py2exe等。

首先,咱們須要安裝pyinstaller:使用pip install pyinstaller。

安裝完成,會在python程序根目錄的Scripts文件夾下生成pyinstaller.exe。

若是在軟件安裝時,沒有將Scripts添加至系統環境變量,須要手動添加到環境變量的PATH中,不然沒法在cmd下使用該命令。

以往很正常的打包,有位朋友反饋使用相同的代碼打包失敗:

pyinstaller報錯


找了半天才發現從python 3.6開始,enum34庫再也不與標準庫兼容。該庫也是沒必要要的,所以只需卸載它便可。
pip uninstall enum34
再次執行打包操做便可。

 

整體代碼

最後附上工具的總體代碼,可供你們參考:

 
1# -*- coding: utf-8 -*-
 2# @Author   : 王翔
 3# @JianShu  : 清風Python
 4# @Date     : 2019/11/14 01:18
 5# @Software : PyCharm
 6# @version  :Python 3.7.3
 7# @File     : ChangeImage.py
 8
 9
10from tkinter.messagebox import *
11from tkinter.filedialog import *
12from tkinter import *
13import base64
14from PIL import Image, ImageFilter, ImageOps
15import os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vgsFXBqQkXWIfDVb7mIB95le/I6ZWcGAV4J5nM76QEFhnFE3H6eNDhinJP9rCEaCxBSBr2I5BCaSWgxKGi45IJANtMJVr3tBa2OrKgch2JbNg0o0DopUkO0e1epbrXqWgWGvKEdDOOKcp2BqaUPg2OabU83GhkRRgEwFOmhYpWf4wppIoV6orRKxOeohZJbLaFITEQDBrEkNSlKrWpTF0qGjAKFk1Wh6qnyslirlMeHc7RO+WRYwwUl7T7zLQ9qBlK8WqENNQ47CfueVhh0oAnnPKncpWTTU+FVBWqEQMrUZGaFFEzhaDBeooi9YvZeFCSohQtho1bzVuJCtYSx4Tnju0JgApWuhO0Gu5wnuVjUc5KFPrgIJf+iZzkouUji2UlagDCKW1Wu7nAsmWwtnWQDDqSl5DQozyrSsleXKcZ1tUuQ7iLXZRe0k7FxZNXxjMIqXxyFqPOaZXRTcOMlOcpzA3DAAuVzVSi4l0ebe5iApHNMB4yqfDVIy37CLnfRW2LoAlRBDcgykT/EqOZEuHgLyk66/L8G5j+fYQ9AETDWUHzsJ8AaihjQINSAmO4d9noPWcFVIyUascxZAJAkBES5qIimOxN668exCt+ByKJg5IxdPJFUGEFstWanOlfj52ReWgZJbYKTkzsBM8CaTk4HS2MlmpFoFhWypmVomaztHwcVqgXJEhiMHtejErYaKvlLXO5y17+MpjDLOYxk7nMZj4zmtOs5jWzuc1ufjOc4yznOSMpIAA7'
18
19
20def dodge(a, b, alpha):
21    return min(int(a * 255 / (256 - b * alpha)), 255)
22
23
24def draw(dir_info, blur=25, alpha=1.0):
25    base_dir = os.path.dirname(os.path.abspath(__file__))
26    save_file_name = os.path.join(base_dir, 'final.png')
27    img = Image.open(dir_info)
28    img1 = img.convert('L')  # 圖片轉換灰色
29    img2 = img1.copy()
30    img2 = ImageOps.invert(img2)
31    for i in range(blur):
32        img2 = img2.filter(ImageFilter.BLUR)
33    width, height = img1.size
34    for x in range(width):
35        for y in range(height):
36            a = img1.getpixel((x, y))
37            b = img2.getpixel((x, y))
38        img1.putpixel((x, y), dodge(a, b, alpha))
39    img1.save(save_file_name)
40    img1.show()
41
42
43class LoginPage(object):
44    def __init__(self, master=None):
45        self.root = master
46        self.root.geometry('%dx%d' % (450, 250))
47        self.page = Frame(self.root)
48        self.Dir = StringVar()
49        self.Port = StringVar()
50        self.path = StringVar()
51        self.dir_info = StringVar()
52        self.create_page()
53
54    def create_page(self):
55        self.page.grid()
56        with open('tmp.gif', 'wb+') as f:
57            f.write(base64.b64decode(img_bs64))
58        self.photo = PhotoImage(file='tmp.gif')
59        os.remove('tmp.gif')
60        Label(self.page, text='''
61
62        公衆號: 清風Python
63        做者  : 王翔 
64        時間  :2019-11-14
65        工具  :Python 3. 7. 3 Tkinter        
66        詳情  : 圖片轉素描畫工具''', justify=LEFT).grid(row=0, column=0, columnspan=2, rowspan=1, stick=NW)
67        Label(self.page, text="圖片路徑").grid(row=3, column=0, sticky=W, pady=5)
68        self.dir_info = Entry(self.page, textvariable=self.path)
69        self.dir_info.grid(row=3, column=1, columnspan=1, padx=20)
70        Button(self.root, text="選擇路徑", command=lambda: self.select_path()).grid(row=0, column=0, sticky=S, padx=20,
71                                                                                pady=5)
72        Label(self.page, image=self.photo).grid(row=0, column=2)
73        Button(self.page, text='轉換', command=self.login_check, width=10).grid(row=3, column=2, padx=10, pady=5)
74
75    def select_path(self):
76        path_ = askopenfilename(filetypes=[("file", "*.*")])
77        self.path.set(path_)
78
79    def login_check(self):
80        img_dir = self.dir_info.get()
81        if img_dir == "":
82            showinfo(title='錯誤', massage='路徑錯誤')
83        else:
84            draw(img_dir)
85
86
87root = Tk()
88root.title('素描畫轉化工具')
89LoginPage(root)
90root.mainloop()
相關文章
相關標籤/搜索