Third commit
This commit is contained in:
69
Image_Capture.py
Normal file
69
Image_Capture.py
Normal 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
|
Reference in New Issue
Block a user