生成渐变色指定长宽图像

生成渐变色指定长宽图像

import cv2
import numpy as np

def create_gradient_image(start_color, end_color, width, height):
    # 将颜色值从[0, 1]的范围转换为[0, 255]的范围
    start_color = [c * 255 for c in start_color]
    end_color = [c * 255 for c in end_color]

    # 创建一个空白图像
    image = np.zeros((height, width, 3), dtype=np.uint8)

    # 计算每个像素的颜色值
    for y in range(height):
        # 计算当前行的颜色值
        r = start_color[0] * (1 - y / (height - 1)) + end_color[0] * (y / (height - 1))
        g = start_color[1] * (1 - y / (height - 1)) + end_color[1] * (y / (height - 1))
        b = start_color[2] * (1 - y / (height - 1)) + end_color[2] * (y / (height - 1))

        # 设置当前行的颜色值
        image[y, :, 0] = r
        image[y, :, 1] = g
        image[y, :, 2] = b

    return image

# 定义起始颜色和结束颜色
start_color = [0.2, 0.4, 0.8]  # 蓝色
end_color = [0.8, 0.2, 0.4]    # 红色

# 定义图像的宽度和高度
width = 1080
height = 1080

# 创建渐变色图像
gradient_image = create_gradient_image(start_color, end_color, width, height)
cv2.imwrite('gradient_image.png',gradient_image)
# # 显示渐变色图像
# cv2.imshow("Gradient Image", gradient_image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
posted @ 2023-06-07 16:26  梁君牧  阅读(22)  评论(0编辑  收藏  举报