所需库
tkinter
PIL
国内镜像加速下载库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tkinter
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
记录
当时看着简单就做滤镜了,看英文看得头大,就直接自己搞滤镜
想着直接搞滤镜太简单,还是加个GUI比较好,而且可以给别人用,就动手了
功能设计
原想功能是直接滤镜做滤镜就好,但是这个方向太笼统了,需要具体一点。
想了想,功能应该有导入原图片,选择滤镜种类,加滤镜,保存处理后图片这几个功能
GUI设计
GUI框包括这几个部分
按钮1:选择图片,搜索后发现有模块可以直接调用文件浏览器选择
# choose picture
button1=Button(root,text='选择图片',width=8,command=Select_pic)
button1.pack()
下拉栏2:选择不同的图像处理模式
# choose filter
cmb=ttk.Combobox(root)
cmb.pack()
# # set the content of the cmb
cmb['value']=('模糊','高斯模糊','锐化','描边','边缘强化','平滑','浮雕','锐化遮罩','转字符画*')
cmb.current(0)
按钮3:开始处理,需要读取下拉框2内的内容然后根据内容处理图片
# start convert
button2=Button(root,text='开始处理',width=8,command=Process_pic)
button2.pack()
按钮4:保存图片
# save pic
button3=Button(root,text='保存图片',width=8,command=Save_pic)
button3.pack()
小的关键点
程序本身两个部分。一部分是图像处理,这里可以调用现成的库,另一部分是GUI,分开来都简单,只用做这个项目的话随便看看入门教程30min-1h可以上手
但是处理好两者之间的关系有一点细节需要注意
因为我想用文件浏览器打开图片,所以调用了tkinter里的filedialog.askopenfilename()
这种方式打开的只是名字
然后用PIL里的Image.open()
参数是名字,打开图片
filedialog其实有个函数叫askopenfile()
但是这种打开不是PIL库能处理的打开,所以采取折中的方式,记录名字,用名字打开图片。
(例程里的img_in.show()只是我debug用的)
遇到的问题
- 参考教程选择图片成功,但是在处理图片的地方用不了:采用
global
标识img_in - 想用switch,但是python里没有switch语句:用
key
记录输入值,用if/elif
判断
成品
未完成版代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Author: ColourfulWhite
# QQ: 1061688677
# Function: Picture Filter GUI
from tkinter import filedialog
from tkinter import Tk
from tkinter import ttk
from tkinter import Button
from tkinter import Label
from PIL import Image
from PIL import ImageFilter
# Create instance
root=Tk()
root.title("Filters of Picture")
root.geometry('600x500')
#functions
def Select_pic():
"""
:parameter: select the picture
:return: show the pic in the left box
"""
global img_in
imgname=filedialog.askopenfilename()
img_in=Image.open(imgname)
img_in.show()
def Process_pic():
"""
:parameter: text in combobox
:return: process the picture & show the pic in the right box
"""
global img_out,img_in
key=cmb.get()
if key=='模糊':
img_out=img_in.convert('RGB').filter(ImageFilter.BLUR)
elif key=='高斯模糊':
img_out=img_in.convert('RGB').filter(ImageFilter.GaussianBlur(radius=2))
elif key=='锐化':
img_out=img_in.convert('RGB').filter(ImageFilter.SHARPEN)
elif key=='描边':
img_out=img_in.convert('RGB').filter(ImageFilter.CONTOUR)
elif key=='边缘强化':
img_out=img_in.convert('RGB').filter(ImageFilter.EDGE_ENHANCE)
elif key=='平滑':
img_out=img_in.convert('RGB').filter(ImageFilter.SMOOTH)
elif key=='浮雕':
img_out=img_in.convert('RGB').filter(ImageFilter.EMBOSS)
elif key=='锐化遮罩':
img_out=img_in.convert('RGB').filter(ImageFilter.UnsharpMask(radius=2,percent=150,threshold=3))
elif key=='转字符画':
pass
# show pic in the right box
img_out.show()
def Save_pic():
pass
# GUI
# pic in
# pic out
right_pic=Label(root)
right_pic.pack()
# choose pic
button1=Button(root,text='选择图片',width=8,command=Select_pic)
button1.pack()
# choose filter
cmb=ttk.Combobox(root)
cmb.pack()
# # set the content of the cmb
cmb['value']=('模糊','高斯模糊','锐化','描边','边缘强化','平滑','浮雕','锐化遮罩','转字符画*')
cmb.current(0)
# # bind function
# start convert
button2=Button(root,text='开始处理',width=8,command=Process_pic)
button2.pack()
# save pic
button3=Button(root,text='保存图片',width=8,command=Save_pic)
button3.pack()
root.mainloop()
不足
理想的效果是想GUI设计里一样的,图片应该在Tk上显示,但是折腾了好久都没成功,还是等我系统学习GUI再来捣鼓吧