Fourth commit
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user