diff --git a/Blue_noise_sampling.py b/Blue_noise_sampling.py index 01cf7fe..a21ca86 100644 --- a/Blue_noise_sampling.py +++ b/Blue_noise_sampling.py @@ -5,46 +5,55 @@ # @File : Blue_noise_sampling.py # @Software: PyCharm -''' -计算新图形(放大后或缩小后)的坐标点像素值对应于原图像中哪一个像素点填充的。 -src是原图,dst是新图,原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例,假如是缩小图片括号内的数字小于1,放大则大于1,相当于系数,再乘以新图片的宽度/高度,就实现了缩放。 -''' -from PIL import Image import matplotlib.pyplot as plt import numpy as np -import math - - -# # 最近邻插值算法 -# # dstH为新图的高;dstW为新图的宽 -# def blueNoiseSampl(img, dstH, dstW): -# scrH, scrW, t = img.shape # src原图的长宽 -# retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8) -# for i in range(dstH - 1): -# for j in range(dstW - 1): -# scrx = round(i * (scrH / dstH)) -# scry = round(j * (scrW / dstW)) -# retimg[i, j] = img[scrx, scry] -# -# return retimg -# -# -# im_path = './data/th.png' -# image = np.array(Image.open(im_path)) -# -# plt.figure(figsize=(16, 8)) -# -# plt.subplot(1, 2, 1) -# plt.imshow(image) -# -# image1 = blueNoiseSampl(image, image.shape[0] * 2, image.shape[1] * 2) -# # 从array转换成image -# image1 = Image.fromarray(image1.astype('uint8')).convert('RGB') -# image1.save('./data/picture13.png') -# plt.subplot(1, 2, 2) -# plt.imshow(image1) -# plt.show() +from PIL import Image + +''' +srcX=newX*(srcW/newW) +srcY=newY*(srcH/newH) +src是原图,dst是新图,原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例, +假如是缩小图片括号内的数字小于1,放大则大于1,相当于系数,再乘以新图片的宽度/高度,就实现了缩放。 +''' + +def blueNoiseSampl(img, newH, newW): + ''' + + :param img: 图片 + :param newH: 新图的高 + :param newW: 新图的宽 + :return: 新图 + ''' + 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(picPath): + ''' + :param picPath: 图片地址 + :return: 样图 + ''' + # 获取图片矩阵 + image = np.array(Image.open(picPath)) + # 设置画布 + plt.figure(figsize=(16, 8)) + # 合并 + plt.subplot(121) + plt.imshow(image) + # 调用采样函数 + image1 = blueNoiseSampl(image, image.shape[0] * 2, image.shape[1] * 2) + # 图片保存 + Image.fromarray(np.uint8(image1)).save("./data/picture13.png") + plt.subplot(122) + plt.imshow(image1) + plt.show() + return image1 diff --git a/GUI.py b/GUI.py index d147813..59580a1 100644 --- a/GUI.py +++ b/GUI.py @@ -1,12 +1,363 @@ # -*- coding: utf-8 -*- -# @Time : 2022-7-25 0025 15:03 +# @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 # 弹窗库 -# git checkout -b "main" +import PIL +import numpy as np +import requests +from PIL import Image, ImageTk -import tkinter -root =tkinter.Tk() +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() diff --git a/GammaCorrection.py b/GammaCorrection.py index dbea8a1..eefd123 100644 --- a/GammaCorrection.py +++ b/GammaCorrection.py @@ -6,34 +6,45 @@ # @Software: PyCharm ''' gamma矫正公式: -f(x)=xγ +f(x)=x^γ 即输出是输入的幂函数,指数为γ. ''' import matplotlib.pyplot as plt import numpy as np from PIL import Image +# 文件保存的地址及文件夹 root = "E:\\桌面\\Python_Picture_Analysis\\data\\" -def gammaCorrect(filePath,val): +# 定义伽马矫正函数 +def gammaCorrect(filePath, val): + ''' + + :param filePath: 图片路径 + :param val: 伽马值 + :return: 图片 + ''' + + # 获取图片 im = Image.open(filePath) - img = np.array(im) + img = np.array(im) # 图片转矩阵 - img1 = np.power(img / float(np.max(img)), 1 / 1.5) - img2 = np.power(img / float(np.max(img)), val) + # 伽马函数使用 + img1 = np.power(img / float(np.max(img)), 1 / 1.5) # gamma值为1/1.5 + img2 = np.power(img / float(np.max(img)), val) # 自定义gamma值 + # 多图合并 plt.subplot(131) plt.imshow(img) - plt.title("origin") + plt.title("origin") # 展示原图 plt.subplot(132) plt.imshow(img1) - plt.title("gammar=1/1.5") + plt.title("gammar=1/1.5") # 展示gamma =1/1.5 plt.subplot(133) plt.imshow(img2) - plt.title("user-defined") + plt.title("user-defined") # 展示自定义 gammat Image plt.show() + # 图片保存 Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg') return img2 - - diff --git a/Image_Capture.py b/Image_Capture.py index 5094d2f..1183cc5 100644 --- a/Image_Capture.py +++ b/Image_Capture.py @@ -9,9 +9,11 @@ import matplotlib.pyplot as plt import numpy as np from PIL import Image +# 图片保存的地址文件夹 root = "E:\\桌面\\Python_Picture_Analysis\\data\\" +# 定义图片剪切函数 def imgCut(path, left, upper, right, lower): """ :param path: 图片路径 @@ -21,27 +23,34 @@ def imgCut(path, left, upper, right, lower): :param lower:与底部边界的距离 :return: """ + # 获取图片 img = Image.open(path) - print(img.size) - part = (left, upper, right, lower) + # print(img.size) Picture Size + part = (left, upper, right, lower) # 设置剪切的大小 + # 剪切函数的调用 cut_img = img.crop(part) + # 展示图片 + # 展示原图 plt.figure("image Cut") plt.subplot(121) plt.title("origin") plt.imshow(img) + # 展示剪切后的图片 plt.subplot(122) plt.title("CUT part") plt.imshow(cut_img) plt.show() + # 图片保存 Image.fromarray(np.uint8(cut_img)).save(root + "picture11.jpg") return cut_img +#定义图片部分替换函数 def imgReplace(path1, path2, left2, upper2, right2, lower2): """ :param path1: 被替换图片的路径 @@ -49,21 +58,27 @@ def imgReplace(path1, path2, left2, upper2, right2, lower2): :param left2, upper2, right2, lower2:替换图片的大小 :return: """ - - img1 = Image.open(path1) - img2 = Image.open(path2) + #图片的获取 + img1 = Image.open(path1) # 被替换图 + img2 = Image.open(path2) #替换图 part2 = (left2, upper2, right2, lower2) cut_img2 = img2.crop(part2) + #调用替换函数 img1.paste(cut_img2, (10, 50, 250, 250)) + #定义画布 plt.figure("picture replace") plt.subplot(121) plt.imshow(img2) + plt.title("Origin") # 展示原图 plt.subplot(122) plt.imshow(img1) + plt.title("replaced picture") #展示替换后的图 plt.show() + + # 保存 替换后的图片 Image.fromarray(np.uint8(img1)).save(root + 'picture12.jpg') return img1 diff --git a/Image_Mix.py b/Image_Mix.py index e55f103..51535d4 100644 --- a/Image_Mix.py +++ b/Image_Mix.py @@ -4,22 +4,36 @@ # @Email : derighoid@gmail.com # @File : Image_Mix.py # @Software: PyCharm - -from PIL import Image +import PIL import matplotlib.pyplot as plt import numpy as np -import cv2 +from PIL import Image + +''' +img1:图片对象1 +img2:图片对象2 +alpha:透明度 ,取值范围为 0 到 1,当取值为 0 时,输出图像相当于 image1 的拷贝,而取值为 1 时, +则是 image2 的拷贝,只有当取值为 0.5 时,才为两个图像的中合。因此改值的大小决定了两个图像的混合程度''' + + # 阿尔法图像混合 def imageMix(imagePath1, imagePath2): + ''' + + :param imagePath1: 混合图片1的地址 + :param imagePath2: 混合图像2的地址 + :return: 混合后的图像 + ''' + + # 获取两张图片 img1 = Image.open(imagePath1) - im1 = np.array(img1) img2 = Image.open(imagePath2) - im2 = np.array(img2) - - img3 = cv2.addWeighted(im1, 0.8, im2, 0.3, 0) + # 调用图片混合函数 + img3 = PIL.Image.blend(img1, img2, 0.5) # 0.5 为gamma 值 + # 展示混合后的图片 plt.imshow(img3) + + # 保存图片 Image.fromarray(np.uint8(img3)).save("./data/picture14.png") plt.show() - - diff --git a/Modify_Pixels.py b/Modify_Pixels.py index 72d5687..2d6625b 100644 --- a/Modify_Pixels.py +++ b/Modify_Pixels.py @@ -41,12 +41,13 @@ class modfifyImage(): # 显示图像三通道颜色 def img_RGB(self): root="E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址 - red = self.imageMatrix().copy() + red = self.imageMatrix().copy() # 图片复制 red[:, :, 1:3] = 0 green = self.imageMatrix().copy() green[:, :, ::2] = 0 blue = self.imageMatrix().copy() blue[:, :, :2] = 0 + # 定义画布 多图合并显示 x, y = plt.subplots(2, 2) x.set_size_inches(10, 10) y[0, 0].imshow(self.imageMatrix()) diff --git a/Orientation_Modification.py b/Orientation_Modification.py index 22e11c3..9e70418 100644 --- a/Orientation_Modification.py +++ b/Orientation_Modification.py @@ -11,7 +11,7 @@ import os import matplotlib.pyplot as plt import numpy as np from PIL import Image - +#定义 图片方向改变类 class directionChanges(): def __init__(self, filePath): self.filePath = filePath @@ -64,10 +64,11 @@ class directionChanges(): # 保存图片 def img_Save(self): - root = "E:\\桌面\\Python_Picture_Analysis\\data\\" - if not os.path.exists(root): - os.mkdir(root) + root = "E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址 + if not os.path.exists(root): # 判断是否已经存在该文件 + os.mkdir(root) #没有就创建 else: + #图片保存 Image.fromarray(np.uint8(self.imageRepetition())).save(root+'picture4'+'.jpg') Image.fromarray(np.uint8(self.mirrorSymmetry())).save(root+'picture5'+'.jpg') Image.fromarray(np.uint8(self.verticalSummetry_right())).save(root+'picture6'+'.jpg') diff --git a/PictureCrawling/Picture _Crawling.py b/PictureCrawling/Picture _Crawling.py new file mode 100644 index 0000000..01e1dc3 --- /dev/null +++ b/PictureCrawling/Picture _Crawling.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# @Time : 2022-7-24 0024 11:18 +# @Author : Qing +# @Email : derighoid@gmail.com +# @File : Picture _Crawling.py +# @Software: PyCharm + +# 导入库 +import os +import requests + + +# path = '(?<=