Python 证件照换底色

# -*- coding: utf-8 -*-
'''
@Time    : 2021/4/12 19:06
@Author  : 水一RAR
'''

import numpy as np
import cv2

girl = cv2.imread('./zjz.jpg')  # 照片地址
# print(girl)
print(girl.shape)

new_girl = cv2.resize(girl, (200, 200))
cv2.imshow('new_girl', new_girl)
rows, cols, _ = new_girl.shape
hsv_img = cv2.cvtColor(new_girl, cv2.COLOR_BGR2HSV)

lower_blue = np.array([78, 43, 46])
high_blue = np.array([124, 255, 255])

mask = cv2.inRange(hsv_img, lower_blue, high_blue)  # 范围内的转换白色,其他位置黑色
print("mask的shape", mask.shape)
for i in range(rows):
    for j in range(cols):
        if mask[i, j] == 255:
            new_girl[i, j] = (0, 0, 255)  # 新图像的底色

cv2.imshow('res', new_girl)
cv2.imwrite('new_girl.jpg', new_girl)  # 存储路径
cv2.waitKey(0)

原文地址:https://www.cnblogs.com/fushengliuyi/p/14649580.html