Third commit

This commit is contained in:
lan daiqing 2022-07-26 21:27:11 +08:00
commit 079dd34037
11 changed files with 554 additions and 0 deletions

50
Blue_noise_sampling.py Normal file
View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 19:09
# @Author : Qing
# @Email : derighoid@gmail.com
# @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()

12
GUI.py Normal file
View File

@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-25 0025 15:03
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : GUI.py
# @Software: PyCharm
import tkinter
# git checkout -b "main"
import tkinter
root =tkinter.Tk()

39
GammaCorrection.py Normal file
View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 11:41
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : GammaCorrection.py
# @Software: PyCharm
'''
gamma矫正公式
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):
im = Image.open(filePath)
img = np.array(im)
img1 = np.power(img / float(np.max(img)), 1 / 1.5)
img2 = np.power(img / float(np.max(img)), val)
plt.subplot(131)
plt.imshow(img)
plt.title("origin")
plt.subplot(132)
plt.imshow(img1)
plt.title("gammar=1/1.5")
plt.subplot(133)
plt.imshow(img2)
plt.title("user-defined")
plt.show()
Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg')
return img2

69
Image_Capture.py Normal file
View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 15:46
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Image_Capture.py
# @Software: PyCharm
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: 图片路径
:param left:与左边界的距离
:param upper:与上边界的距离
:param right:与有边界的距离
:param lower:与底部边界的距离
:return:
"""
img = Image.open(path)
print(img.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: 被替换图片的路径
:param path2: 替换的图片路径
:param left2, upper2, right2, lower2:替换图片的大小
:return:
"""
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.subplot(122)
plt.imshow(img1)
plt.show()
Image.fromarray(np.uint8(img1)).save(root + 'picture12.jpg')
return img1

25
Image_Mix.py Normal file
View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 17:44
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Image_Mix.py
# @Software: PyCharm
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import cv2
# 阿尔法图像混合
def imageMix(imagePath1, imagePath2):
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)
plt.imshow(img3)
Image.fromarray(np.uint8(img3)).save("./data/picture14.png")
plt.show()

65
Modify_Pixels.py Normal file
View File

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-24 0024 16:42
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Modify_Pixels.py
# @Software: PyCharm
import os
# 导入库
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
class modfifyImage():
def __init__(self, filePath):
self.filePath = filePath
# 图片读取
def imageRead(self):
if self.filePath == " ":
print("Please full in the file path")
else:
face = Image.open(self.filePath)
return face
# 图片转矩阵
def imageMatrix(self):
im = np.array(self.imageRead())
return im
# 图片像素反转
def pixInversion(self):
im2 = 255 - self.imageMatrix()
plt.figure()
plt.imshow(im2)
plt.show()
return im2
# 显示图像三通道颜色
def img_RGB(self):
root="E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址
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())
y[0, 1].imshow(red)
y[1, 0].imshow(green)
y[1, 1].imshow(blue)
# 图片保存
Image.fromarray(np.uint8(red)).save(root + 'picture1' + '.jpg')
Image.fromarray(np.uint8(blue)).save(root + 'picture2' + '.jpg')
Image.fromarray(np.uint8(green)).save(root + 'picture3' + '.jpg')
plt.show()
return red,green,blue
# 获得该点的类型
def get_imgType(self, x, y):
return type(self.imageMatrix()[x, y])

View File

@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-25 0025 10:46
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Orientation_Modification.py
# @Software: PyCharm
import os
# 导入库
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
class directionChanges():
def __init__(self, filePath):
self.filePath = filePath
# 图片读取
def imageRead(self):
face3 = Image.open(self.filePath)
return face3
# 图片上矩阵化
def imageMatrix(self):
im3 = np.array(self.imageRead())
return im3
def imageRepetition(self):
# 图像重复
# 先水平方向两次再垂直方向3次
plt1 = np.concatenate((self.imageMatrix(), self.imageMatrix()), axis=1)
plt2 = np.concatenate((plt1, plt1, plt1), axis=0)
plt.imshow(plt2)
plt.show()
# # 先垂直方向转3次,再水平方向转4次
plt3 = np.concatenate((self.imageMatrix(), self.imageMatrix(), self.imageMatrix()), axis=0)
plt4 = np.concatenate((plt3, plt3, plt3, plt3), axis=1)
plt.imshow(plt4)
plt.show()
return plt2
# 图像水平镜面对称
def mirrorSymmetry(self):
plt6 = self.imageMatrix()[::-1]
plt.imshow(plt6)
plt.show()
return plt6
# 垂直对称(右旋转)
def verticalSummetry_right(self):
plt5 = self.imageMatrix().swapaxes(1, 0)
plt.imshow(plt5[:, ::-1])
plt.show()
return plt5
# 垂直镜面(左旋转)
# transpose函数的作用就是调换数组的行列值的索引值类似于求矩阵的转置
def verticalSummetry_lift(self):
plt7 = self.imageMatrix().transpose(1, 0, 2) # x,y轴转置
plt.imshow(plt7[::-1])
plt.show()
return plt7
# 保存图片
def img_Save(self):
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')
Image.fromarray(np.uint8(self.verticalSummetry_lift())).save(root+'picture7'+'.jpg')

36
Picture _Crawling.py Normal file
View File

@ -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 = '(?<=<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)

73
Picture_Reading.py Normal file
View File

@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-24 0024 16:00
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Picture_Reading.py
# @Software: PyCharm
# 导入库
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 == " ":
print("Please full in the file path")
else:
face = Image.open(self.filePath)
return face
# 图片转矩阵
def imageMatrix(self):
im = np.array(self.fileRead())
return im
# 图片转灰色
def imageGrey(self):
im_gray = np.array(self.fileRead().convert("L"))
return im_gray
# 打印图片类型
def get_imageType(self):
print(type(self.imageGrey()))
# 打印图片矩阵元素类型
def get_imageDtype(self):
print(self.imageGrey().dtype)
# 打印图片大小
def get_imageSize(self):
print(self.imageMatrix().shape)
# 图片plt展示
def plt_image(self):
# 原图片显示
plt.imshow(self.imageMatrix())
plt.show()
# 灰色图片显示
plt.imshow(self.imageGrey())
plt.show()
# 图片保存
def image_save(self):
path = 'E:\\桌面\\Python_Picture_Analysis\\data\\numpy_red.png'
root="E:\\桌面\\Python_Picture_Analysis\\data\\"
if not os.path.exists(root):
os.mkdir(root) # 判断是否存在文件夹
else:
if os.path.exists(path): # 判断是否已经存在该文件
print("File already exists")
else:
Image.fromarray(np.uint8(self.imageGrey())).save(path)
print("Save successfully")

60
Subtractive_Algorithm.py Normal file
View File

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-26 0026 9:58
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : Subtractive_Algorithm.py
# @Software: PyCharm
import os.path
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from Picture_Reading import *
class ReduceColor(pictureRead):
def __init__(self,filePath):
self.filePath=filePath
def readImg(self):
face4=Image.open(self.filePath)
return face4
def imageMatrix(self):
im4 = np.array(self.readImg())
return im4
# 减色算法
def decrease_color(self):
out=self.imageMatrix().copy()
out=out//64*64+32
Image.fromarray((np.uint8(out))).save("E:\\桌面\\Python_Picture_Analysis\\data\\picture8.jpg")
return out
# 展示图片
def plt_Img(self):
plt.imshow(self.decrease_color())
plt.show()
#图像二值化(以128为阈值进行二值化)
def imgBinarization(self):
a=self.imageGrey()-np.array([[128]])
b=np.floor(a/np.array([[256]]))
c=b+np.array([[1]],dtype=np.int16)
bfilter=c.astype("uint8")
result=bfilter*np.array([[255]],dtype=np.uint8)
# result=np.array(self.readImg().convert("1"))
plt.imshow(result)
plt.show()
return result
#保存图片
def img_Save(self):
root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
if not os.path.exists(root):
os.mkdir(root)
else:
Image.fromarray(np.uint8(self.decrease_color())).save(root + 'picture8'+'.jpg')
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9'+'.jpg')
print("Picture Save successfully")

49
main.py Normal file
View File

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# @Time : 2022-7-24 0024 15:59
# @Author : Qing
# @Email : derighoid@gmail.com
# @File : main.py
# @Software: PyCharm
from GammaCorrection import *
from Modify_Pixels import *
from Orientation_Modification import *
from Subtractive_Algorithm import *
from Image_Mix import *
if __name__ == '__main__':
# 图片读取
picture_read = pictureRead('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg')
# picture_read.plt_image()
# 图片修改
modif_image = modfifyImage('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg')
# print(modif_image.img_RGB())
# 图片方向修改
direction_Changes = directionChanges('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg')
# 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.jpg')
# reduce_color.plt_Img()
# 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.jpg',20,10,190,190)
# imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\picture.jpg',30,70,230,230)
# 图片替换
# imgReplace('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg', "E:\\桌面\\Python_Picture_Analysis\\data\\picture.jpg",
# 10, 50, 250, 250)
# 阿尔法混合
# imageMix('E:\\桌面\\Python_Picture_Analysis\\data\\th.png','E:\\桌面\\Python_Picture_Analysis\\data\\picture.png')