0. 前言
设置窗口,打开单张图片,对图片进行高斯滤波、边缘锐化、中值滤波、旋转图片、灰度处理几个方面的图像增强处理,最后将处理过的图片保存在本地。
运行界面
对图片处理界面
1.源码
import os
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename
from scipy import ndimage
def act():
print('Start open image')
openfile()
# 高斯滤波
def img_gs():
global cur_img, img_tag, photo
print(cur_img)
cur_img = ndimage.gaussian_filter(cur_img, sigma=1)
cur_img = Image.fromarray(cur_img)
photo = ImageTk.PhotoImage(cur_img)
if img_tag is not None:
canvas.delete(img_tag)
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.image = photo
canvas.update()
# 边缘锐化
def img_br():
global cur_img, img_tag, photo
blurred1 = ndimage.gaussian_filter(cur_img, sigma=1)
blurred3 = ndimage.gaussian_filter(cur_img, sigma=3)
cur_img = blurred3 + 6 * (blurred3 - blurred1)
cur_img = Image.fromarray(cur_img)
photo = ImageTk.PhotoImage(cur_img)
if img_tag is not None:
canvas.delete(img_tag)
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.image = photo
canvas.update()
# 打开文件
def openfile():
global cur_img, photo, img_tag, canvas
path_ = askopenfilename()
path.set(path_)
if path_ is not None:
cur_img = Image.open(entry.get())
image_x, image_y = cur_img.size
photo = ImageTk.PhotoImage(cur_img)
canvas = tk.Canvas(frame, width=image_x, height=image_y, bg="#ffd9b3")
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.grid(row=0, column=0, columnspan=5)
# 中值滤波
def img_zz():
global cur_img, img_tag, photo
cur_img = ndimage.median_filter(cur_img, 7) # 中值滤波
cur_img = Image.fromarray(cur_img)
photo = ImageTk.PhotoImage(cur_img)
if img_tag is not None:
canvas.delete(img_tag)
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.image = photo
canvas.update()
count = 1
def img_sv():
global count
if not os.path.exists('./img'):
os.mkdir('./img')
cur_img.save(f'./img/{count}.jpg')
count += 1
def img_roll():
global cur_img, img_tag, photo
# angle = 45
# cur_img=cur_img.rotate(angle,expand = 1)
cur_img = cur_img.transpose(Image.FLIP_LEFT_RIGHT) # 左右翻转
photo = ImageTk.PhotoImage(cur_img, master=window)
if img_tag is not None:
canvas.delete(img_tag)
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.image = photo
canvas.update()
def img_gray():
global cur_img, photo, img_tag
cur_img = cur_img.convert('L')
photo = ImageTk.PhotoImage(cur_img, master=window)
if img_tag is not None:
canvas.delete(img_tag)
img_tag = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.image = photo
canvas.update()
if __name__ == '__main__':
img_tag = None
photo = None
window = tk.Tk() # Makes main window
window.wm_title("主页") # 标题
window.config(background='#e6b3ff') # 背景色
window.geometry('800x500+300+200') # 窗口大小以及距离屏幕桌上角的坐标
# font = ('字体', '字号',‘样式')
# 字体 Courier Times Helvetica
# 样式 bold(粗体) 默认值为normal(正常粗细)
# italic(斜体) 默认值为roman(正常直立)
# underline(下划线) 默认值为false
# overstrike(删除线)) 默认值为false
font1 = ("楷体", 12, 'bold')
font2 = ("宋体", 12, "italic")
font3 = ("楷体", 16, "underline")
path = tk.StringVar()
entry = tk.Entry(window, font=font2, width=100, textvariable=path,justify='center', state='readonly')
entry.grid(row=0, column=0, columnspan=7)
# cur_img = Image.open(path.get())
button1 = tk.Button(window, text="打开图片", bd=2, command=act, font=font1)
button1.grid(row=1, column=0)
button2 = tk.Button(window, text="高斯滤波", bd=2, command=img_gs, font=font1)
button2.grid(row=1, column=1)
button3 = tk.Button(window, text="边缘锐化", bd=2, command=img_br, font=font1)
button3.grid(row=1, column=2)
button4 = tk.Button(window, text="中值滤波", bd=2, command=img_zz, font=font1)
button4.grid(row=1, column=3)
button5 = tk.Button(window, text='保存图片', bd=2, command=img_sv, font=font1)
button5.grid(row=1, column=6)
button6 = tk.Button(window, text="旋转图片", bd=2, command=img_roll, font=font1)
button6.grid(row=1, column=4)
button7 = tk.Button(window, text="灰度处理", bd=2, command=img_gray, font=font1)
button7.grid(row=1, column=5)
frame = tk.Frame()
frame.grid(row=5, column=0, columnspan=7)
canvas = tk.Canvas(frame, bg="#ffd9b3")
window.mainloop()
2.使用手册
- 点击打开图片按钮,选中一张照片,打开;
- 最上面一行会显示图片在本地所在位置;
- 随机点击高斯滤波、中值滤波、边缘锐化、选择图片、灰度处理五个按钮,会对当前图片进行相应的处理;
- 处理完之后,可以点击保存图片按钮;
- 多次点击保存图片按钮会进行多次保存,保存的路径是./img下;
- 若保存图片的路径不存在,则会创建文件夹来保存处理过的图片。
3.代码解析
if __name__ == '__main__':
img_tag = None
photo = None
window = tk.Tk() # Makes main window
window.wm_title("主页") # 标题
window.config(background='#e6b3ff') # 背景色
window.geometry('800x500+300+200') # 窗口大小以及距离屏幕桌上角的坐标
# font = ('字体', '字号',‘样式')
# 字体 Courier Times Helvetica
# 样式 bold(粗体) 默认值为normal(正常粗细)
# italic(斜体) 默认值为roman(正常直立)
# underline(下划线) 默认值为false
# overstrike(删除线)) 默认值为false
font1 = ("楷体", 12, 'bold')
font2 = ("宋体", 12, "italic")
font3 = ("楷体", 16, "underline")
path = tk.StringVar()
entry = tk.Entry(window, font=font2, width=100, textvariable=path,justify='center', state='readonly')
entry.grid(row=0, column=0, columnspan=7)
# cur_img = Image.open(path.get())
button1 = tk.Button(window, text="打开图片", bd=2, command=act, font=font1)
button1.grid(row=1, column=0)
button2 = tk.Button(window, text="高斯滤波", bd=2, command=img_gs, font=font1)
button2.grid(row=1, column=1)
button3 = tk.Button(window, text="边缘锐化", bd=2, command=img_br, font=font1)
button3.grid(row=1, column=2)
button4 = tk.Button(window, text="中值滤波", bd=2, command=img_zz, font=font1)
button4.grid(row=1, column=3)
button5 = tk.Button(window, text='保存图片', bd=2, command=img_sv, font=font1)
button5.grid(row=1, column=6)
button6 = tk.Button(window, text="旋转图片", bd=2, command=img_roll, font=font1)
button6.grid(row=1, column=4)
button7 = tk.Button(window, text="灰度处理", bd=2, command=img_gray, font=font1)
button7.grid(row=1, column=5)
frame = tk.Frame()
frame.grid(row=5, column=0, columnspan=7)
canvas = tk.Canvas(frame, bg="#ffd9b3")
window.mainloop()
main函数主要是对窗口ui进行设计,canvas变量主要是将选择打开的图片在窗口ui上面进行显示画布;其中,
打开图片 ===> 对应 act函数 和 openfile函数
高斯滤波 ===> 对应 img_gs函数
边缘锐化 ===> 对应 img_br函数
中值滤波 ===> 对应 img_zz函数
旋转图片 ===> 对应 img_roll函数
灰度处理 ===> 对应 img_gray函数
保存图片 ===> 对应 img_sv函数
cur_img变量是指向当前图片,所以设置为全局变量,以便可以进行多重处理叠加;当然,代码还存在一些问题,大家都可以指出。
完成+1 …