Compare commits

...

3 Commits
master ... new

Author SHA1 Message Date
lan daiqing
19fd3d34c5 Upload New File 2022-07-28 13:38:02 +00:00
lan daiqing
493dc7062a Fifth commit 2022-07-28 21:35:42 +08:00
lan daiqing
3d78b6edb3 Fourth commit 2022-07-27 20:41:38 +08:00
12 changed files with 631 additions and 135 deletions

7
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,7 @@
stages:
- test
sonarqube_master_job:
stage: test
script:
- export ENCRYPT_KEY=`echo $CI_PROJECT_URL.git | md5sum | cut -d' ' -f1`
- /opt/sonar-scanner-4.7.0/bin/sonar-scanner -Dsonar.sources=. -Dsonar.projectKey=p$ENCRYPT_KEY

View File

@ -4,47 +4,63 @@
# @Email : derighoid@gmail.com # @Email : derighoid@gmail.com
# @File : Blue_noise_sampling.py # @File : Blue_noise_sampling.py
# @Software: PyCharm # @Software: PyCharm
from pathlib import Path
''' import matplotlib
计算新图形放大后或缩小后的坐标点像素值对应于原图像中哪一个像素点填充的
src是原图dst是新图原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例假如是缩小图片括号内的数字小于1放大则大于1相当于系数再乘以新图片的宽度/高度就实现了缩放
'''
from PIL import Image
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import math 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
# # 最近邻插值算法 # 图片展示函数
# # dstH为新图的高;dstW为新图的宽 def showImage(picPath):
# def blueNoiseSampl(img, dstH, dstW): '''
# scrH, scrW, t = img.shape # src原图的长宽
# retimg = np.zeros((dstH, dstW, 3), dtype=np.uint8) :param picPath: 图片地址
# for i in range(dstH - 1): :return: 样图
# for j in range(dstW - 1): '''
# scrx = round(i * (scrH / dstH)) # 获取图片矩阵
# scry = round(j * (scrW / dstW)) image = np.array(Image.open(picPath))
# retimg[i, j] = img[scrx, scry] # 设置画布
# plt.figure(figsize=(16, 8))
# return retimg # 合并
# plt.subplot(121)
# plt.imshow(image)
# im_path = './data/th.png' # 调用采样函数
# image = np.array(Image.open(im_path)) image1 = blueNoiseSampl(image, image.shape[0] * 2, image.shape[1] * 2)
# # 图片保存
# plt.figure(figsize=(16, 8)) Image.fromarray(np.uint8(image1)).save("./data/picture13.png")
# plt.subplot(122)
# plt.subplot(1, 2, 1) plt.imshow(image1)
# plt.imshow(image) plt.show()
# return image1
# 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()

358
GUI.py
View File

@ -1,12 +1,362 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# @Time : 2022-7-25 0025 15:03 # @Time : 2022-7-27 0027 15:21
# @Author : Qing # @Author : Qing
# @Email : derighoid@gmail.com # @Email : derighoid@gmail.com
# @File : GUI.py # @File : GUI.py
# @Software: PyCharm # @Software: PyCharm
import os
import tkinter import tkinter
import tkinter.filedialog
# 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("基于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)
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,activeforeground="red")
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()),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
mirrorSymmetry_button.place(x=170, y=75)
# 右旋转
def verticalSummetry_right(e_tery):
img = Image.open(e_entry.get())
img2 = img.transpose(Image.ROTATE_270)
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),activeforeground="red")
verticalSummetry_right_button.place(x=240, y=75)
# 左旋转
def verticalSummetry_lift(e_entry):
img = Image.open(e_entry.get())
img2 = img.transpose(Image.ROTATE_90)
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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
imageMix_button.place(x=40, y=105)
# 图像剪切函数
def imageCut(e_entry,Entrybutton): # 一个Entrybutton代表四个值(20, 10, 190, 190)
img1 = Image.open(e_entry.get())
part = (eval(Entrybutton.get()))
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,Entrybutton),activeforeground="red")
cut_button.place(x=110, y=105)
# 部分替换函数
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)
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,Entrybutton),activeforeground="red")
imgReplace_button.place(x=170, y=105)
# 输入框
Entrybutton = tkinter.Entry(root, width=15)
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)), float(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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
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),activeforeground="red")
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()

View File

@ -6,34 +6,45 @@
# @Software: PyCharm # @Software: PyCharm
''' '''
gamma矫正公式 gamma矫正公式
f(x)=xγ f(x)=x^γ
即输出是输入的幂函数指数为γ 即输出是输入的幂函数指数为γ
''' '''
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from PIL import Image from PIL import Image
# 文件保存的地址及文件夹
root = "E:\\桌面\\Python_Picture_Analysis\\data\\" root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
def gammaCorrect(filePath,val): # 定义伽马矫正函数
def gammaCorrect(filePath, val):
'''
:param filePath: 图片路径
:param val: 伽马值
:return: 图片
'''
# 获取图片
im = Image.open(filePath) 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.subplot(131)
plt.imshow(img) plt.imshow(img)
plt.title("origin") plt.title("origin") # 展示原图
plt.subplot(132) plt.subplot(132)
plt.imshow(img1) plt.imshow(img1)
plt.title("gammar=1/1.5") plt.title("gammar=1/1.5") # 展示gamma =1/1.5
plt.subplot(133) plt.subplot(133)
plt.imshow(img2) plt.imshow(img2)
plt.title("user-defined") plt.title("user-defined") # 展示自定义 gammat Image
plt.show() plt.show()
# 图片保存
Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg') Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg')
return img2 return img2

View File

@ -9,9 +9,11 @@ import matplotlib.pyplot as plt
import numpy as np import numpy as np
from PIL import Image from PIL import Image
# 图片保存的地址文件夹
root = "E:\\桌面\\Python_Picture_Analysis\\data\\" root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
# 定义图片剪切函数
def imgCut(path, left, upper, right, lower): def imgCut(path, left, upper, right, lower):
""" """
:param path: 图片路径 :param path: 图片路径
@ -21,27 +23,34 @@ def imgCut(path, left, upper, right, lower):
:param lower:与底部边界的距离 :param lower:与底部边界的距离
:return: :return:
""" """
# 获取图片
img = Image.open(path) img = Image.open(path)
print(img.size) # print(img.size) Picture Size
part = (left, upper, right, lower) part = (left, upper, right, lower) # 设置剪切的大小
# 函数的调用
cut_img = img.crop(part) cut_img = img.crop(part)
# 展示图片
# 展示原图
plt.figure("image Cut") plt.figure("image Cut")
plt.subplot(121) plt.subplot(121)
plt.title("origin") plt.title("origin")
plt.imshow(img) plt.imshow(img)
# 展示剪切后的图片
plt.subplot(122) plt.subplot(122)
plt.title("CUT part") plt.title("CUT part")
plt.imshow(cut_img) plt.imshow(cut_img)
plt.show() plt.show()
# 图片保存
Image.fromarray(np.uint8(cut_img)).save(root + "picture11.jpg") Image.fromarray(np.uint8(cut_img)).save(root + "picture11.jpg")
return cut_img return cut_img
#定义图片部分替换函数
def imgReplace(path1, path2, left2, upper2, right2, lower2): def imgReplace(path1, path2, left2, upper2, right2, lower2):
""" """
:param path1: 被替换图片的路径 :param path1: 被替换图片的路径
@ -49,21 +58,27 @@ def imgReplace(path1, path2, left2, upper2, right2, lower2):
:param left2, upper2, right2, lower2:替换图片的大小 :param left2, upper2, right2, lower2:替换图片的大小
:return: :return:
""" """
#图片的获取
img1 = Image.open(path1) img1 = Image.open(path1) # 被替换图
img2 = Image.open(path2) img2 = Image.open(path2) #替换图
part2 = (left2, upper2, right2, lower2) part2 = (left2, upper2, right2, lower2)
cut_img2 = img2.crop(part2) cut_img2 = img2.crop(part2)
#调用替换函数
img1.paste(cut_img2, (10, 50, 250, 250)) img1.paste(cut_img2, (10, 50, 250, 250))
#定义画布
plt.figure("picture replace") plt.figure("picture replace")
plt.subplot(121) plt.subplot(121)
plt.imshow(img2) plt.imshow(img2)
plt.title("Origin") # 展示原图
plt.subplot(122) plt.subplot(122)
plt.imshow(img1) plt.imshow(img1)
plt.title("replaced picture") #展示替换后的图
plt.show() plt.show()
# 保存 替换后的图片
Image.fromarray(np.uint8(img1)).save(root + 'picture12.jpg') Image.fromarray(np.uint8(img1)).save(root + 'picture12.jpg')
return img1 return img1

View File

@ -4,22 +4,36 @@
# @Email : derighoid@gmail.com # @Email : derighoid@gmail.com
# @File : Image_Mix.py # @File : Image_Mix.py
# @Software: PyCharm # @Software: PyCharm
import PIL
from PIL import Image
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np 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): def imageMix(imagePath1, imagePath2):
'''
:param imagePath1: 混合图片1的地址
:param imagePath2: 混合图像2的地址
:return: 混合后的图像
'''
# 获取两张图片
img1 = Image.open(imagePath1) img1 = Image.open(imagePath1)
im1 = np.array(img1)
img2 = Image.open(imagePath2) img2 = Image.open(imagePath2)
im2 = np.array(img2) # 调用图片混合函数
img3 = PIL.Image.blend(img1, img2, 0.5) # 0.5 为gamma 值
img3 = cv2.addWeighted(im1, 0.8, im2, 0.3, 0)
# 展示混合后的图片
plt.imshow(img3) plt.imshow(img3)
# 保存图片
Image.fromarray(np.uint8(img3)).save("./data/picture14.png") Image.fromarray(np.uint8(img3)).save("./data/picture14.png")
plt.show() plt.show()

View File

@ -41,17 +41,21 @@ class modfifyImage():
# 显示图像三通道颜色 # 显示图像三通道颜色
def img_RGB(self): def img_RGB(self):
root="E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址 root="E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址
red = self.imageMatrix().copy() red = self.imageMatrix().copy() # 图片复制
red[:, :, 1:3] = 0 red[:, :, 1:3] = 0
green = self.imageMatrix().copy() green = self.imageMatrix().copy()
green[:, :, ::2] = 0 green[:, :, ::2] = 0
blue = self.imageMatrix().copy() blue = self.imageMatrix().copy()
blue[:, :, :2] = 0 blue[:, :, :2] = 0
# 定义画布 多图合并显示
x, y = plt.subplots(2, 2) x, y = plt.subplots(2, 2)
x.set_size_inches(10, 10) x.set_size_inches(10, 10)
y[0, 0].imshow(self.imageMatrix()) y[0, 0].imshow(self.imageMatrix())
y[0, 1].imshow(red) y[0, 1].imshow(red)
y[1, 0].imshow(green) y[1, 0].imshow(green)
y[1, 1].imshow(blue) y[1, 1].imshow(blue)
# 图片保存 # 图片保存
Image.fromarray(np.uint8(red)).save(root + 'picture1' + '.jpg') Image.fromarray(np.uint8(red)).save(root + 'picture1' + '.jpg')

View File

@ -11,7 +11,7 @@ import os
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from PIL import Image from PIL import Image
#定义 图片方向改变类
class directionChanges(): class directionChanges():
def __init__(self, filePath): def __init__(self, filePath):
self.filePath = filePath self.filePath = filePath
@ -42,32 +42,32 @@ class directionChanges():
# 图像水平镜面对称 # 图像水平镜面对称
def mirrorSymmetry(self): def mirrorSymmetry(self):
plt6 = self.imageMatrix()[::-1] # plt6 = self.imageMatrix()[::-1]
plt6=self.imageRead().transpose(Image.ROTATE_180)
plt.imshow(plt6) plt.imshow(plt6)
plt.show() plt.show()
return plt6 return plt6
# 垂直对称(右旋转) # 垂直对称(右旋转)
def verticalSummetry_right(self): 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() plt.show()
return plt5 return plt5
# 垂直镜面(左旋转) # 垂直镜面(左旋转)
# transpose函数的作用就是调换数组的行列值的索引值类似于求矩阵的转置
def verticalSummetry_lift(self): def verticalSummetry_lift(self):
plt7 = self.imageMatrix().transpose(1, 0, 2) # x,y轴转置 plt7 = self.imageRead().transpose(Image.ROTATE_90)
plt.imshow(plt7[::-1]) plt.imshow(plt7)
plt.show() plt.show()
return plt7 return plt7
# 保存图片 # 保存图片
def img_Save(self): def img_Save(self):
root = "E:\\桌面\\Python_Picture_Analysis\\data\\" root = "E:\\桌面\\Python_Picture_Analysis\\data\\" # 图片保存地址
if not os.path.exists(root): if not os.path.exists(root): # 判断是否已经存在该文件
os.mkdir(root) os.mkdir(root) #没有就创建
else: else:
#图片保存
Image.fromarray(np.uint8(self.imageRepetition())).save(root+'picture4'+'.jpg') 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.mirrorSymmetry())).save(root+'picture5'+'.jpg')
Image.fromarray(np.uint8(self.verticalSummetry_right())).save(root+'picture6'+'.jpg') Image.fromarray(np.uint8(self.verticalSummetry_right())).save(root+'picture6'+'.jpg')

View File

@ -31,6 +31,6 @@ def pictureCrawl(url):
except: except:
print("Crawl successful") print("Crawl successful")
#图片URL
# 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" 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) pictureCrawl(url)

View File

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

View File

@ -6,17 +6,17 @@
# @Software: PyCharm # @Software: PyCharm
import os.path import os.path
import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from PIL import Image
from Picture_Reading import * from Picture_Reading import *
class ReduceColor(pictureRead): class ReduceColor(pictureRead):
def __init__(self,filePath): def __init__(self, filePath):
self.filePath=filePath self.filePath = filePath
def readImg(self): def readImg(self):
face4=Image.open(self.filePath) face4 = Image.open(self.filePath)
return face4 return face4
def imageMatrix(self): def imageMatrix(self):
@ -25,36 +25,105 @@ class ReduceColor(pictureRead):
# 减色算法 # 减色算法
def decrease_color(self): def decrease_color(self):
out=self.imageMatrix().copy() '''
out=out//64*64+32 val=|32 (0<=var<64)
Image.fromarray((np.uint8(out))).save("E:\\桌面\\Python_Picture_Analysis\\data\\picture8.jpg") |96 (64<=var<128)
return out |160 (128<=var<192)
|224 (192<=var<256)
:return:
'''
img = self.imageMatrix().copy()
# img=img//64*64+32
H = img.shape[0] # 获取图片的高
W = img.shape[1] # 获取图片的宽
# 展示图片 # 创建新的图
def plt_Img(self): newImg = np.zeros((H, W, 3), np.uint8)
plt.imshow(self.decrease_color()) newImg2 = np.zeros((H, W, 3), np.uint8)
newImg3 = 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) # 传给新图片
for i in range(H):
for j in range(W):
for k in range(3):
if img[i, j][k] < 64:
gray = 0
elif img[i, j][k] < 128:
gray = 64
elif img[i, j][k] < 192:
gray = 128
else:
gray = 192
newImg2[i, j][k] = np.uint8(gray)
for i in range(H):
for j in range(W):
for k in range(3):
if newImg3[i, j][k] < 32:
gray = 0
elif newImg3[i, j][k] < 96:
gray = 32
elif newImg3[i, j][k] < 96:
gray = 96
elif newImg3[i, j][k] < 128:
gray = 96
elif newImg3[i,j][k]<160:
gray=128
elif newImg3[i,j][k]<192:
gray=160
elif newImg3[i,j][k]<244:
gray=192
else:
gray = 224
newImg3[i, j][k] = np.uint8(gray)
#定义画布
plt.figure(figsize=(16,8))
plt.subplot(131)
plt.imshow(img)
plt.title("origin")
plt.subplot(132)
plt.imshow(newImg)
plt.title("1")
plt.subplot(133)
plt.imshow(newImg2)
plt.title("2")
# plt.subplot(144)
# plt.imshow(newImg3)
# plt.title("3")
plt.show() plt.show()
return newImg,newImg2,newImg3
# 图像二值化(以128为阈值进行二值化)
#图像二值化(以128为阈值进行二值化)
def imgBinarization(self): def imgBinarization(self):
a=self.imageGrey()-np.array([[128]]) a = self.imageGrey() - np.array([[128]])
b=np.floor(a/np.array([[256]])) b = np.floor(a / np.array([[256]]))
c=b+np.array([[1]],dtype=np.int16) c = b + np.array([[1]], dtype=np.int16)
bfilter=c.astype("uint8") bfilter = c.astype("uint8")
result=bfilter*np.array([[255]],dtype=np.uint8) result = bfilter * np.array([[255]], dtype=np.uint8)
# result=np.array(self.readImg().convert("1")) # result=np.array(self.readImg().convert("1"))
plt.imshow(result) plt.imshow(result)
plt.show() plt.show()
return result return result
#保存图片 # 保存图片
def img_Save(self): def img_Save(self):
root = "E:\\桌面\\Python_Picture_Analysis\\data\\" root = "E:\\桌面\\Python_Picture_Analysis\\data\\"
if not os.path.exists(root): if not os.path.exists(root):
os.mkdir(root) os.mkdir(root)
else: else:
Image.fromarray(np.uint8(self.decrease_color())).save(root + 'picture8'+'.jpg') Image.fromarray(np.uint8(self.decrease_color)).save(root + 'picture8' + '.jpg')
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9'+'.jpg') Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9' + '.jpg')
print("Picture Save successfully") print("Picture Save successfully")

58
main.py
View File

@ -10,40 +10,52 @@ from Modify_Pixels import *
from Orientation_Modification import * from Orientation_Modification import *
from Subtractive_Algorithm import * from Subtractive_Algorithm import *
from Image_Mix import * from Image_Mix import *
from Blue_noise_sampling import *
from Image_Capture import *
if __name__ == '__main__': if __name__ == '__main__':
# 图片读取 # 图片读取
picture_read = pictureRead('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg') 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.jpg') 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.jpg') direction_Changes = directionChanges('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
# direction_Changes.verticalSummetry_right() # direction_Changes.verticalSummetry_right() # 右旋转
# direction_Changes.verticalSummetry_lift() # direction_Changes.verticalSummetry_lift() # 左旋转
# direction_Changes.mirrorSymmetry() # direction_Changes.mirrorSymmetry() # 镜像对称
# direction_Changes.imageRepetition() # direction_Changes.imageRepetition() # 图像重复
# direction_Changes.img_Save() # # direction_Changes.img_Save() # 图像保存
# 减色算法 # 减色算法
reduce_color = ReduceColor('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg') reduce_color = ReduceColor('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
# reduce_color.plt_Img() # reduce_color.decrease_color() # 减色算法
# reduce_color.img_Save() # # reduce_color.img_Save()
# reduce_color.imgBinarization() # reduce_color.imgBinarization() #图像二值化
# Gamma矫正 # Gamma矫正
# gammaCorrect("./data/th.png", 1.5) # gammaCorrect("./data/th.png", 1.5)
#
# 图片截取 # # 图片截取
# left, upper, right, lower # # left, upper, right, lower
# imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg',20,10,190,190) # imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\th.png',20,10,190,190)
# imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\picture.jpg',30,70,230,230) # # imgCut('E:\\桌面\\Python_Picture_Analysis\\data\\picture.png',30,70,230,230)
#
# 图片替换 # # 图片替换
# imgReplace('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg', "E:\\桌面\\Python_Picture_Analysis\\data\\picture.jpg", # imgReplace('E:\\桌面\\Python_Picture_Analysis\\data\\th.png', "E:\\桌面\\Python_Picture_Analysis\\data\\picture.png",
# 10, 50, 250, 250) # 10, 50, 250, 250)
#
# 阿尔法混合 # # 阿尔法混合
# imageMix('E:\\桌面\\Python_Picture_Analysis\\data\\th.png','E:\\桌面\\Python_Picture_Analysis\\data\\picture.png') # imageMix('E:\\桌面\\Python_Picture_Analysis\\data\\th.png','E:\\桌面\\Python_Picture_Analysis\\data\\picture.png')
#
# #采样
showImage("./data/th.png")