Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created August 31, 2024 09:15
Show Gist options
  • Save hirocarma/374124f87addbe7ddbaa5e71f9b8c280 to your computer and use it in GitHub Desktop.
Save hirocarma/374124f87addbe7ddbaa5e71f9b8c280 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 numpy as np
from scipy.spatial import KDTree
height = 200
width = 400
txt_x = 2
txt_y = 2
txt_color = "red"
width_ratio = 0.8
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 rgb_to_hsv(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, ccs="RGB"):
if ccs == "RGB":
color_name = rgb_to_name(color_code)
elif css == "HSV":
color_name = hsv_to_name(color_code)
color_code = hsv_to_rgb(color_code)
else:
print("Invalid CCS")
image = Image.new(ccs, (width, height), color=(color_code))
draw = ImageDraw.Draw(image)
text = 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))
color_save("02", (247, 221, 111))
color_save("03", (75, 128, 253))
color_save("04", (151, 192, 171))
color_save("05", (247, 175, 119))
color_save("06", (216, 227, 151))
color_save("07", (131, 159, 155))
color_save("08", (169, 142, 94))
color_save("09", (252, 238, 191))
color_save("10", (249, 196, 124))
color_save("11", (244, 132, 85))
color_save("12", (233, 159, 124))
color_save("13", (128, 173, 231))
color_save("13", (128, 173, 231))
color_save("14", (153, 120, 80))
color_save("15", (124, 166, 104))
color_save("16", (66, 59, 77))
color_save("17", (221, 199, 253))
color_save("18", (254, 238, 246))
color_save("19", (51, 75, 92))
color_save("20", (160, 160, 148))
color_save("21", (15, 21, 52))
color_save("22", (245, 234, 200))
color_save("24", (221, 225, 227))
color_save("25", (94, 143, 159))
color_save("26", (75, 213, 232))
color_save("27", (210, 246, 253))
color_save("28", (255, 254, 244))
color_save("29", (234, 242, 247))
color_save("34", (255, 255, 255))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment