Created
September 8, 2024 06:44
-
-
Save hirocarma/4a3c1f6bdc6d9994a74d20f64835e820 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from PIL import Image, ImageDraw, ImageFont | |
| import matplotlib.colors as mcolors | |
| import colorsys | |
| import cv2 | |
| import numpy as np | |
| from scipy.spatial import KDTree | |
| height = 100 | |
| width = 600 | |
| txt_x = 2 | |
| txt_y = 2 | |
| txt_color = "white" | |
| width_ratio = 0.9 | |
| font_family = "NotoSansCJK-Regular.ttc" | |
| def find_font_size(text, font, img, target_width_ratio): | |
| tested_font_size = 100 | |
| tested_font = ImageFont.truetype(font, tested_font_size) | |
| x, y, x2, y2 = get_text_size(text, img, tested_font) | |
| estimated_font_size = tested_font_size / ((x2 - x) / img.width) * target_width_ratio | |
| return round(estimated_font_size) | |
| def get_text_size(text, img, font): | |
| img = Image.new("RGB", (img.width, img.height)) | |
| draw = ImageDraw.Draw(img) | |
| return draw.textbbox((img.width, img.height), text, font=font) | |
| def closest_color_name(requested_color): | |
| css4_colors = mcolors.CSS4_COLORS | |
| names = list(css4_colors.keys()) | |
| rgb_values = [mcolors.hex2color(css4_colors[name]) for name in names] | |
| kdtree = KDTree(rgb_values) | |
| distance, index = kdtree.query(requested_color) | |
| return names[index], distance | |
| def rgb_to_name(rgb): | |
| rgb_normalized = tuple([x / 255 for x in rgb]) | |
| color_name, distance = closest_color_name(rgb_normalized) | |
| if not distance == 0: | |
| color_name = color_name + "(approx)" | |
| return color_name | |
| def hsv_to_rgb(hsv): | |
| rgb = tuple( | |
| round(i * 255) | |
| for i in colorsys.hsv_to_rgb(hsv[0] / 360, hsv[1] / 100, hsv[2] / 100) | |
| ) | |
| return rgb | |
| def hsv_to_name(hsv): | |
| rgb = rgb_to_hsv(hsv) | |
| return rgb_to_name(rgb) | |
| def get_concat_v(img1, img2): | |
| dst = Image.new("RGB", (img1.width, img1.height + img2.height)) | |
| dst.paste(img1, (0, 0)) | |
| dst.paste(img2, (0, img1.height)) | |
| return dst | |
| def color_cube(color_code, text=" "): | |
| color_name = rgb_to_name(color_code) | |
| img = Image.new("RGB", (width, height), color=(color_code)) | |
| draw = ImageDraw.Draw(img) | |
| rgb_text = "rgb=[{}, {}, {}]".format(*color_code) | |
| text = rgb_text + ":" + color_name + text | |
| font_size = find_font_size(text, font_family, img, width_ratio) | |
| font = ImageFont.truetype(font_family, font_size) | |
| draw.text((txt_x, txt_y), text, txt_color, font=font) | |
| return img | |
| def main(): | |
| img_v = color_cube((255, 0, 0), " via totsuko") | |
| img = color_cube((255, 165, 0)) | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((255, 255, 0)) | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((0, 255, 0), " via rui") | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((0, 255, 255)) | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((0, 0, 255), " via kimi") | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((128, 0, 128)) | |
| img_v = get_concat_v(img_v, img) | |
| img = color_cube((255, 255, 255), " via shironeko") | |
| img_v = get_concat_v(img_v, img) | |
| fname = "rainbow.jpg" | |
| img_v.save(fname) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment