Skip to content

Instantly share code, notes, and snippets.

@ltlapy
Last active March 13, 2023 07:38
Show Gist options
  • Save ltlapy/9322e5507b51b72094880b556ef84564 to your computer and use it in GitHub Desktop.
Save ltlapy/9322e5507b51b72094880b556ef84564 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# import colorsys
from PIL import Image
# * path of image
img_path = 'sample.png'
# * size of output (maximum square: 13x13 (2886bytes) )
col = 13
row = 13
img = Image.open(img_path)
template = "$[bg.color={}  ]"
img_resized = img.resize((col, row))
px = img_resized.load()
outstr = ""
for i in range(row):
value = 1 - i/row
for j in range(col):
hue = j/col
colorcode = ''.join([format(int(x / 255 * 15), '1x') for x in px[j,i][:3]])
# generates rainbow: https://misskey.io/notes/9abvp2290s
# colorcode = ''.join([format(int(x * 15), 'x') for x in colorsys.hsv_to_rgb(hue, value, 1)])
outstr += template.format(colorcode)
outstr += '\n'
print("length:", len(outstr))
print(outstr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment