# -*- coding: utf-8 -*- # @Time : 2022-7-27 0027 15:21 # @Author : Qing # @Email : derighoid@gmail.com # @File : GUI.py # @Software: PyCharm import os import tkinter import tkinter.filedialog import tkinter.messagebox # 弹窗库 import PIL import numpy as np import requests from PIL import Image, ImageTk root = tkinter.Tk() root.title = 'new' root.geometry('640x500') # 文件选择 def choose_fiel(): selectFileName = tkinter.filedialog.askopenfilename(title='选择文件') # 选择文件 e.set(selectFileName) 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) 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) # 选择文件按钮 submit_button = tkinter.Button(root, text="选择文件", command=choose_fiel) 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") # 爬取按钮 imgCrawling_button = tkinter.Button(root, text="开始爬取", command=lambda: pictureCrawl(entry.get())) 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) # 灰色图按钮 grayPic_button = tkinter.Button(root, text="灰色图", command=lambda: grayPic(e_entry)) 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) # 镜面按钮 mirrorSymmetry_button = tkinter.Button(root, text="镜面对称", command=lambda: mirrorSymmetry(e_entry)) mirrorSymmetry_button.place(x=170, y=75) # 右旋转 def verticalSummetry_right(e_tery): img = Image.open(e_entry.get()) img = np.array(img) img1 = img.swapaxes(1, 0) img2 = img1[:, ::-1] img2 = Image.fromarray(img2) pic = ImageTk.PhotoImage(img2) img = tkinter.Label(image=pic) img.image = pic img.place(x=300, y=150) # 右旋转按钮 verticalSummetry_right_button = tkinter.Button(root, text="右旋转", command=lambda: verticalSummetry_right(e_entry)) verticalSummetry_right_button.place(x=240, y=75) # 左旋转 def verticalSummetry_lift(e_entry): img = Image.open(e_entry.get()) img = np.array(img) img1 = img.transpose(1, 0, 2) # x,y轴转置 img2 = img1[::-1] img2 = Image.fromarray(img2) pic = ImageTk.PhotoImage(img2) img = tkinter.Label(image=pic) img.image = pic img.place(x=300, y=150) # 左旋转按钮 verticalSummetry_lift_button = tkinter.Button(root, text="左旋转", command=lambda: verticalSummetry_lift(e_entry)) 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) # 像素反转 pixInversion_button = tkinter.Button(root, text="像素反转", command=lambda: pixInversion(e_entry)) 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) # 红色图按钮 red_button = tkinter.Button(root, text="红色图", command=lambda: redImage(e_entry)) 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) # 蓝色图按钮 blue_button = tkinter.Button(root, text="蓝色图", command=lambda: blueImage(e_entry)) blue_button.place(x=490, y=75) #绿色图函数 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) # 绿色按钮 green_button = tkinter.Button(root, text='绿色图', command=lambda: greenImage(e_entry)) 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) # 图像混合按钮 imageMix_button = tkinter.Button(root, text="图像混合", command=lambda: imageMix(e_entry)) imageMix_button.place(x=40, y=105) # 图像剪切函数 def imageCut(e_entry): img1 = Image.open(e_entry.get()) part = (20, 10, 190, 190) img2 = img1.crop(part) img3 = ImageTk.PhotoImage(img2) img = tkinter.Label(image=img3) img.image = img3 img.place(x=350, y=150) # 剪切按钮 cut_button = tkinter.Button(root, text="剪 切", command=lambda: imageCut(e_entry)) cut_button.place(x=110, y=105) # 部分替换函数 def imgReplace(e_entry): part = (10, 50, 250, 250) 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) # 部分替换按钮 imgReplace_button = tkinter.Button(root, text="部分替换", command=lambda: imgReplace(e_entry)) imgReplace_button.place(x=170, y=105) # 输入框 Entrybutton = tkinter.Entry(root, width=15) Entrybutton.insert(0, 2) Entrybutton.place(x=490, y=110) lab5 = tkinter.Label(root, text="Gamma:") lab5.place(x=430, y=110) # 伽马矫正函数(默认1/1,5) def gammaCorrect(e_entry, Entrybutton): img = Image.open(e_entry.get()) img = np.array(img) img1 = np.power(img / float(np.max(img)), int(Entrybutton.get())) 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) # 伽马矫正按钮 gammaCorrect_button = tkinter.Button(root, text="伽马矫正", command=lambda: gammaCorrect(e_entry, Entrybutton)) 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) # 减色按钮 decrease_color_button = tkinter.Button(root, text="减色", command=lambda: decrease_color(e_entry)) 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) # 采样按钮 picSampl_button = tkinter.Button(root, text='图片采样', command=lambda: showImage(e_entry)) 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) # 图片展示按钮 submit_button = tkinter.Button(root, text="显示原图", command=lambda: showImg(e_entry)) 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()