imaginator/utils/gradient.py
2024-11-02 14:11:20 +03:00

30 lines
947 B
Python

from typing import List
import numpy as np
from PIL import Image
from enums import LANGUAGE_COLORS
def generate_gradient(language_name: str) -> Image.Image:
# Define start and end colours and image height and width
h, w = 1080, 1920
# Make output image
gradient = np.zeros((h, w, 3), np.uint8)
# Get language name colors
palette = LANGUAGE_COLORS.get(language_name)
if not palette:
raise KeyError("Language not supported")
left_color: List[int] = palette["left"]
right_color: List[int] = palette["right"]
# Fill R, G and B channels with linear gradient between two end colours
gradient[:, :, 0] = np.linspace(left_color[0], right_color[0], w, dtype=np.uint8)
gradient[:, :, 1] = np.linspace(left_color[1], right_color[1], w, dtype=np.uint8)
gradient[:, :, 2] = np.linspace(left_color[2], right_color[2], w, dtype=np.uint8)
# Save result
return Image.fromarray(gradient)