Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
703db063ee |
@ -4,63 +4,47 @@
|
||||
# @Email : derighoid@gmail.com
|
||||
# @File : Blue_noise_sampling.py
|
||||
# @Software: PyCharm
|
||||
from pathlib import Path
|
||||
|
||||
import matplotlib
|
||||
'''
|
||||
计算新图形(放大后或缩小后)的坐标点像素值对应于原图像中哪一个像素点填充的。
|
||||
src是原图,dst是新图,原来的图像宽度/高度除以新图像的宽度/高度可以得到缩放比例,假如是缩小图片括号内的数字小于1,放大则大于1,相当于系数,再乘以新图片的宽度/高度,就实现了缩放。
|
||||
'''
|
||||
|
||||
from PIL import Image
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
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
|
||||
import math
|
||||
|
||||
|
||||
# 图片展示函数
|
||||
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
|
||||
# # 最近邻插值算法
|
||||
# # 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()
|
||||
|
||||
|
||||
|
||||
|
356
GUI.py
356
GUI.py
@ -1,362 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022-7-27 0027 15:21
|
||||
# @Time : 2022-7-25 0025 15:03
|
||||
# @Author : Qing
|
||||
# @Email : derighoid@gmail.com
|
||||
# @File : GUI.py
|
||||
# @Software: PyCharm
|
||||
import os
|
||||
import tkinter
|
||||
import tkinter.filedialog
|
||||
|
||||
import PIL
|
||||
import numpy as np
|
||||
import requests
|
||||
from PIL import Image, ImageTk
|
||||
# git checkout -b "main"
|
||||
|
||||
import tkinter
|
||||
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()
|
||||
|
@ -6,45 +6,34 @@
|
||||
# @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):
|
||||
'''
|
||||
|
||||
: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) # gamma值为1/1.5
|
||||
img2 = np.power(img / float(np.max(img)), val) # 自定义gamma值
|
||||
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.title("origin")
|
||||
plt.subplot(132)
|
||||
plt.imshow(img1)
|
||||
plt.title("gammar=1/1.5") # 展示gamma =1/1.5
|
||||
plt.title("gammar=1/1.5")
|
||||
plt.subplot(133)
|
||||
plt.imshow(img2)
|
||||
plt.title("user-defined") # 展示自定义 gammat Image
|
||||
plt.title("user-defined")
|
||||
plt.show()
|
||||
# 图片保存
|
||||
Image.fromarray(np.uint8(img2)).save(root + 'picture10' + '.jpg')
|
||||
return img2
|
||||
|
||||
|
||||
|
@ -9,11 +9,9 @@ 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: 图片路径
|
||||
@ -23,34 +21,27 @@ def imgCut(path, left, upper, right, lower):
|
||||
:param lower:与底部边界的距离
|
||||
:return:
|
||||
"""
|
||||
# 获取图片
|
||||
img = Image.open(path)
|
||||
# print(img.size) Picture Size
|
||||
part = (left, upper, right, lower) # 设置剪切的大小
|
||||
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: 被替换图片的路径
|
||||
@ -58,27 +49,21 @@ 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
|
||||
|
34
Image_Mix.py
34
Image_Mix.py
@ -4,36 +4,22 @@
|
||||
# @Email : derighoid@gmail.com
|
||||
# @File : Image_Mix.py
|
||||
# @Software: PyCharm
|
||||
import PIL
|
||||
|
||||
from PIL import Image
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
'''
|
||||
img1:图片对象1
|
||||
img2:图片对象2
|
||||
alpha:透明度 ,取值范围为 0 到 1,当取值为 0 时,输出图像相当于 image1 的拷贝,而取值为 1 时,
|
||||
则是 image2 的拷贝,只有当取值为 0.5 时,才为两个图像的中合。因此改值的大小决定了两个图像的混合程度'''
|
||||
|
||||
|
||||
# 图像混合
|
||||
import cv2
|
||||
# 阿尔法图像混合
|
||||
def imageMix(imagePath1, imagePath2):
|
||||
'''
|
||||
|
||||
:param imagePath1: 混合图片1的地址
|
||||
:param imagePath2: 混合图像2的地址
|
||||
:return: 混合后的图像
|
||||
'''
|
||||
|
||||
# 获取两张图片
|
||||
img1 = Image.open(imagePath1)
|
||||
im1 = np.array(img1)
|
||||
img2 = Image.open(imagePath2)
|
||||
# 调用图片混合函数
|
||||
img3 = PIL.Image.blend(img1, img2, 0.5) # 0.5 为gamma 值
|
||||
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()
|
||||
|
||||
|
||||
|
@ -41,21 +41,17 @@ 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())
|
||||
|
||||
y[0, 1].imshow(red)
|
||||
|
||||
y[1, 0].imshow(green)
|
||||
|
||||
y[1, 1].imshow(blue)
|
||||
# 图片保存
|
||||
Image.fromarray(np.uint8(red)).save(root + 'picture1' + '.jpg')
|
||||
|
@ -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
|
||||
@ -42,32 +42,32 @@ class directionChanges():
|
||||
|
||||
# 图像水平镜面对称
|
||||
def mirrorSymmetry(self):
|
||||
# plt6 = self.imageMatrix()[::-1]
|
||||
plt6=self.imageRead().transpose(Image.ROTATE_180)
|
||||
plt6 = self.imageMatrix()[::-1]
|
||||
plt.imshow(plt6)
|
||||
plt.show()
|
||||
return plt6
|
||||
|
||||
# 垂直对称(右旋转)
|
||||
def verticalSummetry_right(self):
|
||||
|
||||
plt5=self.imageRead().transpose(Image.ROTATE_270)
|
||||
|
||||
plt.imshow(plt5)
|
||||
plt5 = self.imageMatrix().swapaxes(1, 0)
|
||||
plt.imshow(plt5[:, ::-1])
|
||||
plt.show()
|
||||
return plt5
|
||||
|
||||
# 垂直镜面(左旋转)
|
||||
# transpose函数的作用就是调换数组的行列值的索引值,类似于求矩阵的转置
|
||||
def verticalSummetry_lift(self):
|
||||
plt7 = self.imageRead().transpose(Image.ROTATE_90)
|
||||
plt.imshow(plt7)
|
||||
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) #没有就创建
|
||||
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')
|
||||
|
@ -31,6 +31,6 @@ def pictureCrawl(url):
|
||||
except:
|
||||
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"
|
||||
pictureCrawl(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"
|
||||
# pictureCrawl(url)
|
@ -7,13 +7,17 @@
|
||||
|
||||
# 导入库
|
||||
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 == " ":
|
||||
@ -21,7 +25,8 @@ class pictureRead(object):
|
||||
else:
|
||||
face = Image.open(self.filePath)
|
||||
return face
|
||||
# 图片数字化
|
||||
|
||||
# 图片转矩阵
|
||||
def imageMatrix(self):
|
||||
im = np.array(self.fileRead())
|
||||
return im
|
||||
@ -45,14 +50,11 @@ class pictureRead(object):
|
||||
|
||||
# 图片plt展示
|
||||
def plt_image(self):
|
||||
plt.subplot(121)
|
||||
# 原图片显示
|
||||
plt.imshow(self.imageMatrix())
|
||||
plt.title("Original")
|
||||
plt.subplot(122)
|
||||
plt.show()
|
||||
# 灰色图片显示
|
||||
plt.imshow(self.imageGrey())
|
||||
plt.title('Gray')
|
||||
plt.show()
|
||||
|
||||
# 图片保存
|
||||
|
@ -6,11 +6,11 @@
|
||||
# @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
|
||||
@ -25,86 +25,16 @@ class ReduceColor(pictureRead):
|
||||
|
||||
# 减色算法
|
||||
def decrease_color(self):
|
||||
'''
|
||||
val=|32 (0<=var<64)
|
||||
|96 (64<=var<128)
|
||||
|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] # 获取图片的宽
|
||||
out=self.imageMatrix().copy()
|
||||
out=out//64*64+32
|
||||
Image.fromarray((np.uint8(out))).save("E:\\桌面\\Python_Picture_Analysis\\data\\picture8.jpg")
|
||||
return out
|
||||
|
||||
# 创建新的图
|
||||
newImg = np.zeros((H, W, 3), np.uint8)
|
||||
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")
|
||||
# 展示图片
|
||||
def plt_Img(self):
|
||||
plt.imshow(self.decrease_color())
|
||||
plt.show()
|
||||
return newImg,newImg2,newImg3
|
||||
|
||||
|
||||
#图像二值化(以128为阈值进行二值化)
|
||||
def imgBinarization(self):
|
||||
@ -124,6 +54,7 @@ class ReduceColor(pictureRead):
|
||||
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.decrease_color())).save(root + 'picture8'+'.jpg')
|
||||
Image.fromarray(np.uint8(self.imgBinarization())).save(root + 'picture9'+'.jpg')
|
||||
print("Picture Save successfully")
|
||||
|
||||
|
58
main.py
58
main.py
@ -10,52 +10,40 @@ from Modify_Pixels import *
|
||||
from Orientation_Modification import *
|
||||
from Subtractive_Algorithm import *
|
||||
from Image_Mix import *
|
||||
from Blue_noise_sampling import *
|
||||
from Image_Capture import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 图片读取
|
||||
picture_read = pictureRead('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
|
||||
#图片展示
|
||||
picture_read = pictureRead('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg')
|
||||
# picture_read.plt_image()
|
||||
|
||||
|
||||
# 图片修改
|
||||
modif_image = modfifyImage('E:\\桌面\\Python_Picture_Analysis\\data\\th.png')
|
||||
# modif_image.img_RGB()
|
||||
# modif_image.pixInversion()
|
||||
modif_image = modfifyImage('E:\\桌面\\Python_Picture_Analysis\\data\\th.jpg')
|
||||
# print(modif_image.img_RGB())
|
||||
|
||||
# 图片方向修改
|
||||
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 = 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.png')
|
||||
# reduce_color.decrease_color() # 减色算法
|
||||
# # reduce_color.img_Save()
|
||||
# reduce_color.imgBinarization() #图像二值化
|
||||
|
||||
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.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",
|
||||
|
||||
# 图片截取
|
||||
# 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')
|
||||
#
|
||||
# #采样
|
||||
showImage("./data/th.png")
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user