Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created August 31, 2024 15:35
Show Gist options
  • Save hirocarma/6d1b127e0f24636163a9ea60d42980b3 to your computer and use it in GitHub Desktop.
Save hirocarma/6d1b127e0f24636163a9ea60d42980b3 to your computer and use it in GitHub Desktop.
#!/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 = 400
txt_x = 2
txt_y = 2
txt_color = "red"
width_ratio = 0.9
font_family = "NotoSansCJK-Regular.ttc"
def find_font_size(text, font, image, target_width_ratio):
tested_font_size = 100
tested_font = ImageFont.truetype(font, tested_font_size)
x, y, x2, y2 = get_text_size(text, image, tested_font)
estimated_font_size = (
tested_font_size / ((x2 - x) / image.width) * target_width_ratio
)
return round(estimated_font_size)
def get_text_size(text, image, font):
im = Image.new("RGB", (image.width, image.height))
draw = ImageDraw.Draw(im)
return draw.textbbox((image.width, image.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 color_save(text, color_code, width):
color_name = rgb_to_name(color_code)
width = int(width)
image = Image.new("RGB", (width, height), color=(color_code))
draw = ImageDraw.Draw(image)
rgb_text = "rgb=[{}, {}, {}]".format(*color_code)
text = text + ": " + rgb_text # + ":" + color_name
font_size = find_font_size(text, font_family, image, width_ratio)
font = ImageFont.truetype(font_family, font_size)
draw.text((txt_x, txt_y), text, txt_color, font=font)
name = text + ".jpg"
image.save(name)
def main():
color_save("01", (244, 233, 227), 199)
color_save("02", (247, 221, 111), 79)
color_save("03", (75, 128, 253), 62)
color_save("04", (151, 192, 171), 48.640)
color_save("05", (247, 175, 119), 54.867)
color_save("06", (216, 227, 151), 88)
color_save("07", (131, 159, 155), 54.213)
color_save("08", (169, 142, 94), 34.747)
color_save("09", (252, 238, 191), 14.80)
color_save("10", (249, 196, 124), 38.147)
color_save("11", (244, 132, 85), 59.160)
color_save("12", (233, 159, 124), 37.187)
color_save("13", (128, 173, 231), 42.987)
color_save("14", (153, 120, 80), 57.947)
color_save("15", (124, 166, 104), 43.320)
color_save("16", (66, 59, 77), 117)
color_save("17", (221, 199, 253), 79)
color_save("18", (254, 238, 246), 102)
color_save("19", (51, 75, 92), 31.760)
color_save("20", (160, 160, 148), 31.933)
color_save("21", (15, 21, 52), 59.27)
color_save("22", (245, 234, 200), 58.693)
color_save("23", (0, 0, 0), 89)
color_save("24", (221, 225, 227), 45.253)
color_save("25", (94, 143, 159), 62)
color_save("26", (75, 213, 232), 191)
color_save("27", (210, 246, 253), 85)
color_save("28", (255, 254, 244), 225)
color_save("29", (234, 242, 247), 78)
color_save("30", (0, 0, 0), 151)
color_save("31", (0, 0, 0), 192)
color_save("32", (0, 0, 0), 159)
color_save("33", (0, 0, 0), 122)
color_save("34", (255, 255, 255), 86)
image_h.save("bar.jpg")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment