Python开发GUI实战:图片转换素描画工具!
The following article is from 清风Python Author 王翔丨
奋斗没有终点
好好学习72变,因为将来
没有人能替你阻挡81难 。
生如蝼蚁,当有鸿鹄之志;
命如纸薄,应有不屈之心 。
今天被这句话触动了,所以开篇分享给大家。鸡汤有毒,但有时大家却靠它激励自己继续前行!
python开发GUI
程序员的自我救赎,使用python开发性格分析工具 这篇文章,通过Python编写了GUI的性格分析工具。一位从我公众号开始结识的朋友,询问关于如何快速编写一个exe工具的问题。由于功能简单且之前无相关GUI编程基础,为了快速完成开发,我向他推荐了easygui模块。
python作为胶水语言,几乎没有不能做的事情,但个人一直觉得在GUI开发方面,python可以算作是短板了,为什么?因为性能…python的性能问题,往往出现在其他编程语言对其的鄙夷中。但不管如何python在GUI编程上,也是有大量优秀模块的。
也许有些人不知道这个模块,但如果你安装python后,使用过自带的IDLE,那么你就应该了解它。tkinter作为python自带的GUI模块,IDLE便是通过该模块开发的,也是我比较喜欢的一个模块。
easygui
第一次了解这个模块,还是在小甲鱼的《零基础入门学习python》时学习的,它对Tkinter进行了二次封装,使得GUI操作变得更为简单,但同时也局限于表层的使用,无法自定义更优雅的实现方案。
wxpython是一个Python对于wxWidgets(这是用 C++ 编写)包装后的产物,算是个流行的跨平台GUI工具包。wxpython的优势在于运行速度较快,但编写起来较为复杂,而且界面美化效果较差…当然有第三方开发的wxFormBuiler拖拽编辑器,能替代部分的底层代码实现。不在乎外观但追求速度的朋友,可以考虑它。
如果你对GUI编程非常感谢兴趣,可以用心学学这个模块,但如果你只是想快速的实现一些GUI的功能,我更推荐大家学习tkinter模块,为什么?
tkinter作为python默认自带的模块库,无需单独安装
tkinter相比于wxpython与pyqt在制作小工具的方面,更为高效
tkinter在网上存在大量的快速入门教程
tkinter实战
为了证明tkinter的方便与高效,今天来编写一个将图片转化为素描画的小工具,先给大家看看实现效果:
避坑指南
大家看到,tktiner中附带了一个我的微信二维码,这里只是为了演示tkinter添加图片,没有丝毫推广的味道,哈哈…这里要注意下,tktiner在添加图片时,PhotoImage(file='tmp.gif')要求图片必须是.gif结尾的图片,否则就会报错:
_tkinter.TclError: couldn’t recognize data in image file
大家看到我没有加载图片,而是通过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下使用该命令。
以往很正常的打包,有位朋友反馈使用相同的代码打包失败:
找了半天才发现从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
16
17img_bs64 = b'R0lGODlhwADBAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOA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U+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()
END
公众号回复素描,即可获取整套代码及打包好的exe文件。
欢迎访问我的github:
https://github.com/BreezePython
推荐阅读: