所需资源文件:
图片文件下载地址
# -*- coding: utf-8 -*-
import cv2
def main():
# 1.读取图片 打印图片形状
img_src = cv2.imread('./image/Lenna.png', cv2.IMREAD_UNCHANGED)
img_copy = img_src.copy()
print('Lenna 图片形状:', img_src.shape)
logo = cv2.imread('./image/opencv_logo.png', cv2.IMREAD_UNCHANGED)
logo = cv2.resize(logo, (20, 20))
print('opencv_logo 图片形状:', logo.shape)
butterfly = cv2.imread('./image/butterfly.jpg', cv2.IMREAD_UNCHANGED)
butterfly = cv2.resize(butterfly, (20, 20))
print('butterfly 图片形状:', butterfly.shape)
# 2.读取 坐标 y, x的值并打印
y = 100
x = 50
(b, g, r) = img_src[y, x]
print('蓝色:', b, '蓝色:', g, '红色:', r)
# 3.将 Logo butterfly 图片贴在原图上
img_src[100:100 + logo.shape[0], 300:300 + logo.shape[1]] = logo[:, :, 0:3] # 两张图片的shape不一样
img_src[300:300 + butterfly.shape[1], 100:100 + butterfly.shape[0]] = butterfly[:, :, 0:3]
# 4.使用文字标识 坐标值
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_src, text='X=0,Y=0', org=(0, 0), fontFace=font, fontScale=0.5,
color=(0, 255, 0), thickness=2, bottomLeftOrigin=True)
cv2.putText(img_src, text='X=10,Y=30', org=(10, 30), fontFace=font, fontScale=0.5,
color=(0, 255, 0), thickness=2)
cv2.putText(img_src, text='X=100,Y=300', org=(100, 300), fontFace=font, fontScale=0.5,
color=(0, 255, 0), thickness=2)
cv2.putText(img_src, text='X=300,Y=100', org=(300, 100), fontFace=font, fontScale=0.5,
color=(0, 255, 0), thickness=2)
# 5.显示结果图片 移动图片位置
cv2.imshow('src', img_copy)
cv2.moveWindow('src', 0, 0)
cv2.imshow('img+logo', img_src)
cv2.moveWindow('img+logo', x=img_src.shape[1], y=0)
# 6.写入图片
cv2.imwrite('img_logo.jpg', img_src)
cv2.waitKey(0)
if __name__ == '__main__':
main()