Python+numpy draw a grayscale image of pure color, and put another image in the middle

#! /usr/bin/env python
# coding=utf-8

import numpy as np
import cv2
path = "C:/Users/LQL/Desktop/draw.png"

image = cv2.imread(path)
print(image.shape)
(x, y,_) = image.shape
gray0=np.zeros((1080,1920,3),dtype=np.uint8)
gray0[:,:]=195	# 改变背景灰度值
gray255=gray0[:,:]

x1 = 540-x//2-1 if x%2 != 0 else 540-x//2   # 防止下面像素为奇数,像素区域不对应的情况
y1 = 960-y//2-1 if y%2 != 0 else 960-y//2

gray0[x1:540+x//2, y1:960+y//2] = image

cv2.imshow("Result", gray255)
cv2.waitKey(0)

Original picture:
Insert picture description here
Effect picture:Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45371989/article/details/109276260