Fifth commit

This commit is contained in:
lan daiqing 2022-07-28 21:35:42 +08:00
parent 3d78b6edb3
commit 493dc7062a
10 changed files with 93 additions and 122 deletions

View File

@ -4,13 +4,17 @@
# @Email : derighoid@gmail.com
# @File : Blue_noise_sampling.py
# @Software: PyCharm
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
'''
最近邻插值法
在放大图像时多出来的像素点由最近邻的像素点构成
计算新图形放大后或缩小后的坐标点像素值对应于原图像中哪一个像素点填充的
srcX=newX*(srcW/newW)
srcY=newY*(srcH/newH)
src是原图dst是新图原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例
@ -57,3 +61,6 @@ def showImage(picPath):
plt.imshow(image1)
plt.show()
return image1

75
GUI.py
View File

@ -7,7 +7,6 @@
import os
import tkinter
import tkinter.filedialog
import tkinter.messagebox # 弹窗库
import PIL
import numpy as np
@ -15,16 +14,18 @@ import requests
from PIL import Image, ImageTk
root = tkinter.Tk()
root.title = 'new'
root.geometry('640x500')
root.title("基于Python的图像处理")
root.geometry('640x500')
root.resizable(width=0, height=0)
# 文件选择
def choose_fiel():
selectFileName = tkinter.filedialog.askopenfilename(title='选择文件') # 选择文件
e.set(selectFileName)
#文件选择标签
label1 = tkinter.Label(root, text="文件路径:")
label1.place(x=40, y=0)
@ -37,12 +38,13 @@ 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 = tkinter.Button(root, text="选择文件", command=choose_fiel,activeforeground="red")
submit_button.place(x=520, y=0)
@ -68,7 +70,7 @@ def pictureCrawl(url):
# 爬取按钮
imgCrawling_button = tkinter.Button(root, text="开始爬取", command=lambda: pictureCrawl(entry.get()))
imgCrawling_button = tkinter.Button(root, text="开始爬取", command=lambda: pictureCrawl(entry.get()),activeforeground="red")
imgCrawling_button.place(x=520, y=30)
@ -83,7 +85,7 @@ def grayPic(e_entry):
# 灰色图按钮
grayPic_button = tkinter.Button(root, text="灰色图", command=lambda: grayPic(e_entry))
grayPic_button = tkinter.Button(root, text="灰色图", command=lambda: grayPic(e_entry),activeforeground="red")
grayPic_button.place(x=110, y=75)
@ -100,17 +102,15 @@ def mirrorSymmetry(e_entry):
# 镜面按钮
mirrorSymmetry_button = tkinter.Button(root, text="镜面对称", command=lambda: mirrorSymmetry(e_entry))
mirrorSymmetry_button = tkinter.Button(root, text="镜面对称", command=lambda: mirrorSymmetry(e_entry),activeforeground="red")
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)
img2 = img.transpose(Image.ROTATE_270)
pic = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=pic)
@ -119,19 +119,15 @@ def verticalSummetry_right(e_tery):
# 右旋转按钮
verticalSummetry_right_button = tkinter.Button(root, text="右旋转", command=lambda: verticalSummetry_right(e_entry))
verticalSummetry_right_button = tkinter.Button(root, text="右旋转", command=lambda: verticalSummetry_right(e_entry),activeforeground="red")
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)
img2 = img.transpose(Image.ROTATE_90)
pic = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=pic)
img.image = pic
@ -139,7 +135,7 @@ def verticalSummetry_lift(e_entry):
# 左旋转按钮
verticalSummetry_lift_button = tkinter.Button(root, text="左旋转", command=lambda: verticalSummetry_lift(e_entry))
verticalSummetry_lift_button = tkinter.Button(root, text="左旋转", command=lambda: verticalSummetry_lift(e_entry),activeforeground="red")
verticalSummetry_lift_button.place(x=300, y=75)
@ -155,7 +151,7 @@ def pixInversion(e_entry):
# 像素反转
pixInversion_button = tkinter.Button(root, text="像素反转", command=lambda: pixInversion(e_entry))
pixInversion_button = tkinter.Button(root, text="像素反转", command=lambda: pixInversion(e_entry),activeforeground="red")
pixInversion_button.place(x=360, y=75)
@ -171,7 +167,7 @@ def redImage(e_entry):
# 红色图按钮
red_button = tkinter.Button(root, text="红色图", command=lambda: redImage(e_entry))
red_button = tkinter.Button(root, text="红色图", command=lambda: redImage(e_entry),activeforeground="red")
red_button.place(x=430, y=75)
@ -187,10 +183,11 @@ def blueImage(e_entry):
# 蓝色图按钮
blue_button = tkinter.Button(root, text="蓝色图", command=lambda: blueImage(e_entry))
blue_button = tkinter.Button(root, text="蓝色图", command=lambda: blueImage(e_entry),activeforeground="red")
blue_button.place(x=490, y=75)
#绿色图函数
# 绿色图函数
def greenImage(e_entry):
img = Image.open(e_entry.get())
img = np.array(img)
@ -203,7 +200,7 @@ def greenImage(e_entry):
# 绿色按钮
green_button = tkinter.Button(root, text='绿色图', command=lambda: greenImage(e_entry))
green_button = tkinter.Button(root, text='绿色图', command=lambda: greenImage(e_entry),activeforeground="red")
green_button.place(x=550, y=75)
@ -221,14 +218,14 @@ def imageMix(e_entry):
# 图像混合按钮
imageMix_button = tkinter.Button(root, text="图像混合", command=lambda: imageMix(e_entry))
imageMix_button = tkinter.Button(root, text="图像混合", command=lambda: imageMix(e_entry),activeforeground="red")
imageMix_button.place(x=40, y=105)
# 图像剪切函数
def imageCut(e_entry):
def imageCut(e_entry,Entrybutton): # 一个Entrybutton代表四个值(20, 10, 190, 190)
img1 = Image.open(e_entry.get())
part = (20, 10, 190, 190)
part = (eval(Entrybutton.get()))
img2 = img1.crop(part)
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
@ -237,13 +234,13 @@ def imageCut(e_entry):
# 剪切按钮
cut_button = tkinter.Button(root, text="剪 切", command=lambda: imageCut(e_entry))
cut_button = tkinter.Button(root, text="剪 切", command=lambda: imageCut(e_entry,Entrybutton),activeforeground="red")
cut_button.place(x=110, y=105)
# 部分替换函数
def imgReplace(e_entry):
part = (10, 50, 250, 250)
def imgReplace(e_entry,Entrybutton): # Entrybutton 代表4个值(10, 50, 250, 250)
part = (eval(Entrybutton.get()))
img1 = Image.open(e_entry.get())
img2 = Image.open("./data/picture.png")
img3 = img2.crop(part)
@ -256,23 +253,24 @@ def imgReplace(e_entry):
# 部分替换按钮
imgReplace_button = tkinter.Button(root, text="部分替换", command=lambda: imgReplace(e_entry))
imgReplace_button = tkinter.Button(root, text="部分替换", command=lambda: imgReplace(e_entry,Entrybutton),activeforeground="red")
imgReplace_button.place(x=170, y=105)
# 输入框
Entrybutton = tkinter.Entry(root, width=15)
Entrybutton.insert(0, 2)
Entrybutton.insert(0, 0.5)
Entrybutton.place(x=490, y=110)
lab5 = tkinter.Label(root, text="Gamma:")
lab5.place(x=430, y=110)
lab6=tkinter.Label(root,text=":<-other")
lab6.place(x=585,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()))
img1 = np.power(img / float(np.max(img)), float(Entrybutton.get()))
img2 = Image.fromarray(np.uint8(img1 * 255))
img3 = ImageTk.PhotoImage(img2)
img = tkinter.Label(image=img3)
@ -281,7 +279,7 @@ def gammaCorrect(e_entry, Entrybutton):
# 伽马矫正按钮
gammaCorrect_button = tkinter.Button(root, text="伽马矫正", command=lambda: gammaCorrect(e_entry, Entrybutton))
gammaCorrect_button = tkinter.Button(root, text="伽马矫正", command=lambda: gammaCorrect(e_entry, Entrybutton),activeforeground="red")
gammaCorrect_button.place(x=240, y=105)
@ -308,7 +306,7 @@ def decrease_color(e_entry):
# 减色按钮
decrease_color_button = tkinter.Button(root, text="减色", command=lambda: decrease_color(e_entry))
decrease_color_button = tkinter.Button(root, text="减色", command=lambda: decrease_color(e_entry),activeforeground="red")
decrease_color_button.place(x=310, y=105)
@ -338,7 +336,7 @@ def showImage(e_entry):
# 采样按钮
picSampl_button = tkinter.Button(root, text='图片采样', command=lambda: showImage(e_entry))
picSampl_button = tkinter.Button(root, text='图片采样', command=lambda: showImage(e_entry),activeforeground="red")
picSampl_button.place(x=360, y=105)
@ -352,8 +350,9 @@ def showImg(e_entry):
img.place(x=50, y=150)
# 图片展示按钮
submit_button = tkinter.Button(root, text="显示原图", command=lambda: showImg(e_entry))
submit_button = tkinter.Button(root, text="显示原图", command=lambda: showImg(e_entry),activeforeground="red")
submit_button.place(x=40, y=75)
lable3 = tkinter.Label(root, text='原图', bg='red')

View File

@ -28,7 +28,7 @@ def imgCut(path, left, upper, right, lower):
# print(img.size) Picture Size
part = (left, upper, right, lower) # 设置剪切的大小
# 剪切函数的调用
# 函数的调用
cut_img = img.crop(part)
# 展示图片

View File

@ -16,7 +16,7 @@ alpha透明度 ,取值范围为 0 到 1当取值为 0 时,输出图像
则是 image2 的拷贝只有当取值为 0.5 才为两个图像的中合因此改值的大小决定了两个图像的混合程度'''
# 阿尔法图像混合
# 图像混合
def imageMix(imagePath1, imagePath2):
'''

View File

@ -51,8 +51,11 @@ class modfifyImage():
x, y = plt.subplots(2, 2)
x.set_size_inches(10, 10)
y[0, 0].imshow(self.imageMatrix())
y[0, 1].imshow(red)
y[1, 0].imshow(green)
y[1, 1].imshow(blue)
# 图片保存
Image.fromarray(np.uint8(red)).save(root + 'picture1' + '.jpg')

View File

@ -42,26 +42,25 @@ class directionChanges():
# 图像水平镜面对称
def mirrorSymmetry(self):
plt6 = self.imageMatrix()[::-1]
# plt6 = self.imageMatrix()[::-1]
plt6=self.imageRead().transpose(Image.ROTATE_180)
plt.imshow(plt6)
plt.show()
return plt6
# 垂直对称(右旋转)
def verticalSummetry_right(self):
plt5 = self.imageMatrix().swapaxes(1, 0)
plt.imshow(plt5[:, ::-1])
plt5=self.imageRead().transpose(Image.ROTATE_270)
plt.imshow(plt5)
plt.show()
return plt5
# 垂直镜面(左旋转)
# transpose函数的作用就是调换数组的行列值的索引值类似于求矩阵的转置
def verticalSummetry_lift(self):
plt7 = self.imageMatrix().transpose(1, 0, 2) # x,y轴转置
plt.imshow(plt7[::-1])
plt7 = self.imageRead().transpose(Image.ROTATE_90)
plt.imshow(plt7)
plt.show()
return plt7
# 保存图片
def img_Save(self):
root = "E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址

View File

@ -1,36 +0,0 @@
# -*- 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 = '(?<=<img(?:.*)\s+src=")[^"]+(\.png|jpg|jpeg)'
# 定义函数
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")
# url = "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"
# pictureCrawl(url)

View File

@ -7,17 +7,13 @@
# 导入库
import os.path
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
# 建立图片读取类
class pictureRead(object):
def __init__(self, filePath):
self.filePath = filePath
# 图片读取
def fileRead(self):
if self.filePath == " ":
@ -25,8 +21,7 @@ class pictureRead(object):
else:
face = Image.open(self.filePath)
return face
# 图片转矩阵
# 图片数字化
def imageMatrix(self):
im = np.array(self.fileRead())
return im
@ -53,9 +48,11 @@ class pictureRead(object):
plt.subplot(121)
# 原图片显示
plt.imshow(self.imageMatrix())
plt.title("Original")
plt.subplot(122)
# 灰色图片显示
plt.imshow(self.imageGrey())
plt.title('Gray')
plt.show()
# 图片保存

View File

@ -88,21 +88,21 @@ class ReduceColor(pictureRead):
#定义画布
plt.figure(figsize=(16,8))
plt.subplot(141)
plt.subplot(131)
plt.imshow(img)
plt.title("origin")
plt.subplot(142)
plt.subplot(132)
plt.imshow(newImg)
plt.title("1")
plt.subplot(143)
plt.subplot(133)
plt.imshow(newImg2)
plt.title("2")
plt.subplot(144)
plt.imshow(newImg3)
plt.title("3")
# plt.subplot(144)
# plt.imshow(newImg3)
# plt.title("3")
plt.show()
return newImg,newImg2,newImg3

50
main.py
View File

@ -17,43 +17,45 @@ if __name__ == '__main__':
# 图片读取
picture_read = pictureRead('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
#图片展示
picture_read.plt_image()
# picture_read.plt_image()
# 图片修改
modif_image = modfifyImage('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
# print(modif_image.img_RGB())
# modif_image.img_RGB()
# modif_image.pixInversion()
# 图片方向修改
direction_Changes = directionChanges('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
# direction_Changes.verticalSummetry_right()
# direction_Changes.verticalSummetry_lift()
# direction_Changes.mirrorSymmetry()
# direction_Changes.imageRepetition()
# direction_Changes.img_Save()
# direction_Changes.verticalSummetry_right() # 右旋转
# direction_Changes.verticalSummetry_lift() # 左旋转
# direction_Changes.mirrorSymmetry() # 镜像对称
# direction_Changes.imageRepetition() # 图像重复
# # direction_Changes.img_Save() # 图像保存
# 减色算法
reduce_color = ReduceColor('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
# reduce_color.decrease_color()
# reduce_color.img_Save()
# reduce_color.imgBinarization()
# reduce_color.decrease_color() # 减色算法
# # reduce_color.img_Save()
# reduce_color.imgBinarization() #图像二值化
# Gamma矫正
# gammaCorrect("./data/th.png", 1.5)
# 图片截取
# left, upper, right, lower
imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\th.png',20,10,190,190)
imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\picture.png',30,70,230,230)
# 图片替换
imgReplace('E:\\桌面\\Python_Picture_Analysis\\data\\th.png', "E:\\桌面\\Python_Picture_Analysis\\data\\picture.png",
10, 50, 250, 250)
# 阿尔法混合
imageMix('E:\\桌面\\Python_Picture_Analysis\\data\\th.png','E:\\桌面\\Python_Picture_Analysis\\data\\picture.png')
#蓝噪
#
# # 图片截取
# # left, upper, right, lower
# imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\th.png',20,10,190,190)
# # imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\picture.png',30,70,230,230)
#
# # 图片替换
# imgReplace('E:\\桌面\\Python_Picture_Analysis\\data\\th.png', "E:\\桌面\\Python_Picture_Analysis\\data\\picture.png",
# 10, 50, 250, 250)
#
# # 阿尔法混合
# imageMix('E:\\桌面\\Python_Picture_Analysis\\data\\th.png','E:\\桌面\\Python_Picture_Analysis\\data\\picture.png')
#
# #采样
showImage("./data/th.png")