不多说别的,直接看代码:
```python
import cv2
class Change():
def __init__(self, head_img):
self.head_img = head_img
def make(self):
head_img = self.head_img
# 读取图案
img_head = cv2.imread(head_img)
img_flag = cv2.imread('./timg.png')
# 获取图案宽度
w_head, h_head = img_head.shape[:2]
w_flag, h_flag = img_flag.shape[:2]
# 计算图案缩放比例
scale = w_head / w_flag / 4
# 缩放图案
img_flag = cv2.resize(img_flag, (0, 0), fx=scale, fy=scale)
# 创建一个窗口
cv2.namedWindow("Image")
# 在窗口显示图片
cv2.imshow("Image", img_flag)
# 执行
cv2.waitKey(0)
# 获取缩放后新宽度
w_flag, h_flag = img_flag.shape[:2]
# 按3个通道合并图片
for c in range(0, 3):
img_head[w_head - w_flag:, h_head - h_flag:, c] = img_flag[:, :, c]
# 保存最终结果
cv2.imwrite(self.head_img.split('.')[0] + '.jpg', img_head)
return 'ok'