pythonpictureanalysis/GUI.py

363 lines
10 KiB
Python
Raw Permalink Normal View History

2022-07-26 21:27:11 +08:00
# -*- coding: utf-8 -*-
2022-07-27 20:41:38 +08:00
# @Time : 2022-7-27 0027 15:21
2022-07-26 21:27:11 +08:00
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : GUI.py
# @Software: PyCharm
2022-07-27 20:41:38 +08:00
import os
2022-07-26 21:27:11 +08:00
import tkinter
2022-07-27 20:41:38 +08:00
import tkinter.filedialog
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
import PIL
import numpy as np
import requests
from PIL import Image, ImageTk
2022-07-26 21:27:11 +08:00
2022-07-27 20:41:38 +08:00
root = tkinter.Tk()
2022-07-28 21:35:42 +08:00
root.title("基于Python的图像处理")
root.geometry('640x500')
root.resizable(width=0, height=0)
2022-07-27 20:41:38 +08:00
# 文件选择
def choose_fiel():
selectFileName = tkinter.filedialog.askopenfilename(title='选择文件') # 选择文件
e.set(selectFileName)
2022-07-28 21:35:42 +08:00
#文件选择标签
2022-07-27 20:41:38 +08:00
label1 = tkinter.Label(root, text="文件路径:")
label1.place(x=40, y=0)
e = tkinter.StringVar()
e_entry = tkinter.Entry(root, width=60, textvariable=e)
e_entry.place(x=100, y=0)
lable2 = tkinter.Label(root, text="URL:")
lable2.place(x=40, y=30)
# 设置输入框
entry = tkinter.Entry(root, width=60)
2022-07-28 21:35:42 +08:00
#设置默认值
2022-07-27 20:41:38 +08:00
entry.insert(0,
"https://tse2-mm.cn.bing.net/th/id/OIP-C.fT7uKiT7V7YO2PPINFeOdQHaJ4?w=186&h=248&c=7&r=0&o=5&dpr=1.25&pid=1.7")
entry.place(x=100, y=30)
# 选择文件按钮
2022-07-28 21:35:42 +08:00
submit_button = tkinter.Button(root, text="选择文件", command=choose_fiel,activeforeground="red")
2022-07-27 20:41:38 +08:00
submit_button.place(x=520, y=0)
# 爬取函数
def pictureCrawl(url):
# 文件保存地址
root = "E://桌面//Python_Picture_Analysis//data//"
# 文件的保存地址以及格式
path = root + url.split('/')[-3] + '.png'
try:
if not os.path.exists(root): # 判断是否存在文件夹
os.mkdir(root)
if not os.path.exists(path): # 判断是否存在该文件
r = requests.get(url)
# 文件写入
with open(path, 'wb') as fp:
fp.write(r.content)
print("OK")
else:
print("File already exists")
except:
print("Crawl successful")
# 爬取按钮
2022-07-28 21:35:42 +08:00
imgCrawling_button = tkinter.Button(root, text="开始爬取", command=lambda: pictureCrawl(entry.get()),activeforeground="red")
2022-07-27 20:41:38 +08:00
imgCrawling_button.place(x=520, y=30)
# 图片变灰
def grayPic(e_entry):
img = Image.open(e_entry.get())
img = img.convert('L')
showPic = ImageTk.PhotoImage(img)
img = tkinter.Label(image=showPic)
img.image = showPic
img.place(x=350, y=150)
# 灰色图按钮
2022-07-28 21:35:42 +08:00
grayPic_button = tkinter.Button(root, text="灰色图", command=lambda: grayPic(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
grayPic_button.place(x=110, y=75)
# 图像水平镜面对称
def mirrorSymmetry(e_entry):
img = Image.open(e_entry.get())
img = np.asarray(img)
img1 = np.flipud(img)
img2 = Image.fromarray(img1)
pic = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=pic)
img.image = pic
img.place(x=350, y=150)
# 镜面按钮
2022-07-28 21:35:42 +08:00
mirrorSymmetry_button = tkinter.Button(root, text="镜面对称", command=lambda: mirrorSymmetry(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
mirrorSymmetry_button.place(x=170, y=75)
# 右旋转
def verticalSummetry_right(e_tery):
img = Image.open(e_entry.get())
2022-07-28 21:35:42 +08:00
img2 = img.transpose(Image.ROTATE_270)
2022-07-27 20:41:38 +08:00
pic = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=pic)
img.image = pic
img.place(x=300, y=150)
# 右旋转按钮
2022-07-28 21:35:42 +08:00
verticalSummetry_right_button = tkinter.Button(root, text="右旋转", command=lambda: verticalSummetry_right(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
verticalSummetry_right_button.place(x=240, y=75)
# 左旋转
def verticalSummetry_lift(e_entry):
img = Image.open(e_entry.get())
2022-07-28 21:35:42 +08:00
img2 = img.transpose(Image.ROTATE_90)
2022-07-27 20:41:38 +08:00
pic = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=pic)
img.image = pic
img.place(x=300, y=150)
# 左旋转按钮
2022-07-28 21:35:42 +08:00
verticalSummetry_lift_button = tkinter.Button(root, text="左旋转", command=lambda: verticalSummetry_lift(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
verticalSummetry_lift_button.place(x=300, y=75)
# 像素反转函数
def pixInversion(e_entry):
img = Image.open(e_entry.get())
img2 = 255 - np.array(img)
img2 = Image.fromarray(img2)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 像素反转
2022-07-28 21:35:42 +08:00
pixInversion_button = tkinter.Button(root, text="像素反转", command=lambda: pixInversion(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
pixInversion_button.place(x=360, y=75)
def redImage(e_entry):
img = Image.open(e_entry.get())
img = np.array(img)
img[:, :, 1:3] = 0
img2 = Image.fromarray(img)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 红色图按钮
2022-07-28 21:35:42 +08:00
red_button = tkinter.Button(root, text="红色图", command=lambda: redImage(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
red_button.place(x=430, y=75)
def blueImage(e_entry):
img = Image.open(e_entry.get())
img = np.array(img)
img[:, :, :2] = 0
img2 = Image.fromarray(img)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 蓝色图按钮
2022-07-28 21:35:42 +08:00
blue_button = tkinter.Button(root, text="蓝色图", command=lambda: blueImage(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
blue_button.place(x=490, y=75)
2022-07-28 21:35:42 +08:00
# 绿色图函数
2022-07-27 20:41:38 +08:00
def greenImage(e_entry):
img = Image.open(e_entry.get())
img = np.array(img)
img[:, :, ::2] = 0
img2 = Image.fromarray(img)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 绿色按钮
2022-07-28 21:35:42 +08:00
green_button = tkinter.Button(root, text='绿色图', command=lambda: greenImage(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
green_button.place(x=550, y=75)
# 图像混合函数
def imageMix(e_entry):
img1 = Image.open("./data/picture.png")
img2 = Image.open(e_entry.get())
img3 = PIL.Image.blend(img1, img2, 0.5)
img4 = ImageTk.PhotoImage(img3)
img = tkinter.Label(image=img4)
img.image = img4
img.place(x=350, y=150)
# 图像混合按钮
2022-07-28 21:35:42 +08:00
imageMix_button = tkinter.Button(root, text="图像混合", command=lambda: imageMix(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
imageMix_button.place(x=40, y=105)
# 图像剪切函数
2022-07-28 21:35:42 +08:00
def imageCut(e_entry,Entrybutton): # 一个Entrybutton代表四个值(20, 10, 190, 190)
2022-07-27 20:41:38 +08:00
img1 = Image.open(e_entry.get())
2022-07-28 21:35:42 +08:00
part = (eval(Entrybutton.get()))
2022-07-27 20:41:38 +08:00
img2 = img1.crop(part)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 剪切按钮
2022-07-28 21:35:42 +08:00
cut_button = tkinter.Button(root, text="剪 切", command=lambda: imageCut(e_entry,Entrybutton),activeforeground="red")
2022-07-27 20:41:38 +08:00
cut_button.place(x=110, y=105)
# 部分替换函数
2022-07-28 21:35:42 +08:00
def imgReplace(e_entry,Entrybutton): # Entrybutton 代表4个值(10, 50, 250, 250)
part = (eval(Entrybutton.get()))
2022-07-27 20:41:38 +08:00
img1 = Image.open(e_entry.get())
img2 = Image.open("./data/picture.png")
img3 = img2.crop(part)
img1.paste(img3, (10, 50, 250, 250))
img4 = ImageTk.PhotoImage(img1)
img = tkinter.Label(image=img4)
img.image = img4
img.place(x=350, y=150)
# 部分替换按钮
2022-07-28 21:35:42 +08:00
imgReplace_button = tkinter.Button(root, text="部分替换", command=lambda: imgReplace(e_entry,Entrybutton),activeforeground="red")
2022-07-27 20:41:38 +08:00
imgReplace_button.place(x=170, y=105)
# 输入框
Entrybutton = tkinter.Entry(root, width=15)
2022-07-28 21:35:42 +08:00
Entrybutton.insert(0, 0.5)
2022-07-27 20:41:38 +08:00
Entrybutton.place(x=490, y=110)
lab5 = tkinter.Label(root, text="Gamma:")
lab5.place(x=430, y=110)
2022-07-28 21:35:42 +08:00
lab6=tkinter.Label(root,text=":<-other")
lab6.place(x=585,y=110)
2022-07-27 20:41:38 +08:00
# 伽马矫正函数(默认1/1,5)
def gammaCorrect(e_entry, Entrybutton):
img = Image.open(e_entry.get())
img = np.array(img)
2022-07-28 21:35:42 +08:00
img1 = np.power(img / float(np.max(img)), float(Entrybutton.get()))
2022-07-27 20:41:38 +08:00
img2 = Image.fromarray(np.uint8(img1 * 255))
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 伽马矫正按钮
2022-07-28 21:35:42 +08:00
gammaCorrect_button = tkinter.Button(root, text="伽马矫正", command=lambda: gammaCorrect(e_entry, Entrybutton),activeforeground="red")
2022-07-27 20:41:38 +08:00
gammaCorrect_button.place(x=240, y=105)
def decrease_color(e_entry):
img = Image.open(e_entry.get())
img = np.array(img)
H = img.shape[0] # 获取图片的高
W = img.shape[1] # 获取图片的宽
newImg = np.zeros((H, W, 3), np.uint8)
for i in range(H):
for j in range(W):
for k in range(3): # RGB三分量
if img[i, j][k] < 128:
gray = 0
else:
gray = 128
newImg[i, j][k] = np.uint8(gray) # 传给新图片
newImg = Image.fromarray(newImg)
img3 = ImageTk.PhotoImage(newImg)
img = tkinter.Label(image=img3)
img.image = img3
img.place(x=350, y=150)
# 减色按钮
2022-07-28 21:35:42 +08:00
decrease_color_button = tkinter.Button(root, text="减色", command=lambda: decrease_color(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
decrease_color_button.place(x=310, y=105)
# 采样函数
def picSampl(img, newH, newW):
scrH, scrW, t = img.shape # 原图的长宽
retimg = np.zeros((newH, newW, 3), dtype=np.uint8) # 生成 newH* newW *3 的零矩阵
for i in range(newH - 1):
for j in range(newW - 1):
scrx = round(i * (scrH / newH)) # round对其四舍五入
scry = round(j * (scrW / newW))
retimg[i, j] = img[scrx, scry] # new image
return retimg
# 采样展示
def showImage(e_entry):
img = np.array(Image.open(e_entry.get()))
img1 = picSampl(img, img.shape[0] * 2, img.shape[1] * 2)
img1 = Image.fromarray(img1)
img2 = ImageTk.PhotoImage(img1)
img = tkinter.Label(image=img2)
img.image = img2
img.place(x=350, y=150)
# 采样按钮
2022-07-28 21:35:42 +08:00
picSampl_button = tkinter.Button(root, text='图片采样', command=lambda: showImage(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
picSampl_button.place(x=360, y=105)
# 图片展示
def showImg(e_entry):
load = Image.open(e_entry.get())
render = ImageTk.PhotoImage(load)
img = tkinter.Label(image=render)
img.image = render
img.place(x=50, y=150)
2022-07-28 21:35:42 +08:00
2022-07-27 20:41:38 +08:00
# 图片展示按钮
2022-07-28 21:35:42 +08:00
submit_button = tkinter.Button(root, text="显示原图", command=lambda: showImg(e_entry),activeforeground="red")
2022-07-27 20:41:38 +08:00
submit_button.place(x=40, y=75)
lable3 = tkinter.Label(root, text='原图', bg='red')
lable3.place(x=140, y=460)
lable4 = tkinter.Label(root, text='处理后', bg='red')
lable4.place(x=450, y=460)
root.mainloop()