交🗡证的制作
1. 前期准备
- python3.8+
- cv2库&numpy库&PIL库
- Pycharm or Anaconda编译环境
- 原始交🗡证、头像、ID
![在这里插入图片描述](https://img-blog.csdnimg.cn/b15c01e8fd2a4a4da035e29bf203cb9c.png)
2.代码
import numpy as np
import cv2 as cv
from PIL import Image,ImageDraw,ImageFont
def refresh(img_path,icon_path,ID):
'''
img_path:原交🗡证的路径
icon_path:新头像的路径
ID:新ID(str)
'''
#STEP1 更改ID
path = img_path
img = cv.imread(path)
img[130:180,100:300] = [255,255,255]
#转为PIL格式
cv2img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
pilimg = Image.fromarray(cv2img)
draw = ImageDraw.Draw(pilimg)
font = ImageFont.truetype('simhei.ttf',40,encoding='utf-8')
word = 'ID: '
word += ID
draw.text((30,120) , word , (0,0,0) , font = font)
cv2img2 = cv.cvtColor(np.array(pilimg),cv.COLOR_RGB2BGR)
cv.imwrite('new'+path,cv2img2)
#STEP2 更改头像
new_path = 'new'+path #此处为修改ID后的图片,临时存储
img = cv.imread(new_path)
icon = cv.imread(icon_path)
resized_icon = cv.resize(icon,dsize=(200,200))
img[0:280, 405:600] = [255,255,255]
img[50:250, 455:655] = resized_icon
cv.imwrite(new_path,img)
if __name__ == '__main__':
refresh('card.png','bz.jpg','111藤酒丸')
3. 效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/332e7ae886564554856a7d30b755399f.png)