渐变图像叠加python处理
Posted 良风抚旧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了渐变图像叠加python处理相关的知识,希望对你有一定的参考价值。
'''
Author: TuZhou
Version: 1.0
Date: 2021-08-09 10:46:45
LastEditTime: 2021-10-02 14:10:41
LastEditors: TuZhou
Description:
FilePath: \\python_test\\test.py
'''
import cv2
import numpy as np
from PIL import Image
def opencv_process():
#1:1原头像尺寸
image_size = 960
#需要渐变的guoqi图像,若宽比较长,可以用此处截取
new_flag = cv2.imread('./images/flag2.png')
flag_shape = new_flag.shape
print('Image size', flag_shape)
w = flag_shape[0]
h = flag_shape[1]
min_length = w if w < h else h
new_flag = new_flag[0:min_length, 0:min_length]
# print('New flag', new_flag.shape)
# cv2.imshow('final', new_flag)
# cv2.waitKey(0)
#若是1:1guoqi渐变图像,用此处
#img1 = cv2.resize(cv2.imread('./images/flag1.png'), (image_size, image_size)) #guoqi图片
img2 = cv2.resize(cv2.imread('./images/1.jpg'), (image_size, image_size)) #微信头像
img1 = cv2.resize(new_flag, (image_size, image_size))
middle = int(image_size*0.8) #渐变截至位置(0-image_size)
#np.linspace用于切分,如在1和0之间切割middle份,0-1之间与原图相乘使图像慢慢变淡,guoqi图像从左到右慢慢变淡
#Numpy的tile()函数,就是将原矩阵横向、纵向地复制,参数2为横向复制多少倍,纵向复制多少倍,1表示不复制
#np.newaxis相当于None,这里相当于扩维
#np.repeat在指定维度复制3倍,这里是第2维度变为3,相当于RGB
mask1 = np.repeat(np.tile(np.linspace(1, 0, middle), (img1.shape[1], 1))[:, :, np.newaxis], 3, axis=2)
mask2 = np.repeat(np.tile(np.linspace(0, 1, middle), (img1.shape[1], 1))[:, :, np.newaxis], 3, axis=2)
ones = np.ones((image_size, image_size-middle,3))
zero = np.zeros((image_size, image_size-middle,3))
#与全0矩阵拼接表示渐变图像右边部分消失
mask1 = np.concatenate((mask1,zero), axis=1)
mask2 = np.concatenate((mask2,ones), axis=1)
#final = img1 + img2
final = np.uint8(img1 * mask1 + img2 * mask2)
#final = cv2.resize(final, (128,128))
cv2.imwrite('final.png',final)
cv2.imshow('final', final)
cv2.waitKey(0)
if __name__ == '__main__':
opencv_process()
以上是关于渐变图像叠加python处理的主要内容,如果未能解决你的问题,请参考以下文章