需求:拿到一批数据集(做人脸识别的),由于是回流的数据图片,左上角和右上角分别有个截取的人脸小图,实际想用这张图,但是不需要上面的人脸小图,需要把左上右上的图扣掉,或者覆盖掉
import cv2, os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
def do_image(filepath):
"""
批量处理一个文件夹下的图片,去掉左上,和右上角的人脸
:param filepath:
:return:
"""
file2 = r"test.jpg"
files = os.listdir(filepath)
for file in files:
file1 = filepath + file
print(file1)
img= Image.open(file1)
img2=Image.open(file2)
img2 = img.crop((50, 550, 550, 1100))
# img.paste(img2, (0, 0, 500, 600))
img.paste(img2, (0, 0))
img2 = img.crop((3000, 50, 4500, 600))
img.paste(img2, (3500, 0))
# plt.imshow(img2)
# plt.imshow(img)
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
# cv2.imwrite(file1, img) #中文乱码,用下面的
cv2.imencode('.jpg', img)[1].tofile(file1)
plt.show()
filepath = "E:\Code\xx\\"
do_image(filepath)
处理后:左上角的,用它下面的部分Corp出来,然后覆盖,