您的当前位置:首页正文

签名抠图!python

2024-11-09 来源:个人技术集锦
import cv2
import numpy as np
import time

def gen_sig(img):
    name = str(time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())) + '.png'
    # 设置阈值
    temp = img[:, :, 0]  # 取图像第一个通道
    mmax = np.max(temp)
    mmin = np.min(temp)
    thresh = mmax * 0.5 + mmin * 0.5 - 15

    result = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
    B, G, R, _ = cv2.split(result)

    _, R = cv2.threshold(R, thresh, 255, cv2.THRESH_BINARY)
    _, Alpha = cv2.threshold(R, thresh, 255, cv2.THRESH_BINARY_INV)

    result[:, :, 3] = Alpha

    cv2.imwrite(name, result)

def main(path):
    img = cv2.imread(path)
    gen_sig(img)

if __name__ == '__main__':
    # 载入原图,并转化为灰度图像
    path = r'./2.jpg'
    main(path)

签名抠图小代码,直接转为白色背景插入word,方便得一匹!再也不用花钱抠图啦!

显示全文