Skip to content

Instantly share code, notes, and snippets.

@st98
Last active March 25, 2019 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st98/424970b7d78361234eac66d77de84e6f to your computer and use it in GitHub Desktop.
Save st98/424970b7d78361234eac66d77de84e6f to your computer and use it in GitHub Desktop.
選択すると画像が出てくるやつ
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