Last active
May 10, 2022 05:44
-
-
Save inaniwaudon/48a3cbf5b34e784c1d37e0c7f7e66367 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
from PIL import Image, ImageDraw, ImageFont | |
import argparse | |
import numpy as np | |
list = "筑波大学情報学群情報メディア創成学類つくばだいがくじょうほうめでぃあそうせいがくるい" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("src") | |
parser.add_argument("-f", "--font", default="FOT-SeuratPro-DB.otf") | |
args = parser.parse_args() | |
img = Image.open(args.src) | |
width = 80 | |
height = int(img.height * (width / img.width) * 0.85) | |
img = img.resize((width, height)) | |
size = 50 | |
font = ImageFont.truetype(args.font, size) | |
char_denses = set() | |
for char in list: | |
char_img = Image.new("RGB", (size, size), (255, 255, 255)) | |
char_draw = ImageDraw.Draw(char_img) | |
char_draw.text((0, 0), char, (0, 0, 0), font) | |
blacks = np.count_nonzero(np.asarray(char_img) == 0) | |
char_denses.add((char, blacks)) | |
char_denses = sorted(char_denses, key=lambda i: i[1]) | |
img_array = np.asarray(img) | |
gray = 0.299 * img_array[:,:,2] + 0.587 * img_array[:,:,1] + 0.114 * img_array[:,:,0] | |
for y in range(height): | |
for x in range(width): | |
index = int(round(gray[y][x] / 255 * (len(char_denses) - 1))) | |
print(char_denses[index][0], end="") | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mosaic-image