Last active
March 25, 2019 16:34
-
-
Save st98/424970b7d78361234eac66d77de84e6f to your computer and use it in GitHub Desktop.
選択すると画像が出てくるやつ
This file contains 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
import binascii | |
import os | |
import string | |
import sys | |
import random | |
from PIL import Image | |
TEMPLATE = ''' | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>{title}</title> | |
<style> | |
body {{ font-family: Consolas, monospace; font-size: 8px; }} | |
::selection {{ background: #fff; }} | |
{style} | |
</style> | |
</head> | |
<body> | |
<div> | |
{div} | |
</div> | |
</body> | |
</html> | |
'''.strip() | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print(f'usage: python {sys.argv[0]} <img>') | |
sys.exit(1) | |
im = Image.open(sys.argv[1]).quantize(64).convert('RGB') | |
w, h = im.size | |
pix = im.load() | |
table = {} | |
style = [] | |
for _, (r, g, b) in im.getcolors(): | |
k = 'c' + binascii.hexlify(os.urandom(4)).decode() | |
table[r, g, b] = k | |
style.append(f''' .{k}::selection {{ background: rgb({r}, {g}, {b}); color: rgba(0, 0, 0, 0); }}''') | |
div = '' | |
for y in range(h): | |
for x in range(w): | |
c = ''.join(random.sample(string.ascii_letters, 2)) | |
cl = table[pix[x, y]] | |
div += f'<span class="{cl}">{c}</span>' | |
div += '<br>' | |
with open(f'{sys.argv[1]}.html', 'w') as f: | |
f.write(TEMPLATE.format(title=sys.argv[1], style='\n'.join(style), div=div)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment