from PIL import Image, ImageDraw, ImageFont import argparse import numpy as np
sample_rate = 0.4
def ascii_art(file): im = Image.open(file)
font = ImageFont.load_default() aspect_ratio = font.getsize("x")[0] / font.getsize("x")[1] new_im_size = np.array( [im.size[0] * sample_rate, im.size[1] * sample_rate * aspect_ratio] ).astype(int)
im = im.resize(tuple(new_im_size))
im_color = np.array(im)
im = im.convert("L")
im = np.array(im)
symbols = np.array(list(" .-vM"))
im = (im - im.min()) / (im.max() - im.min()) * (symbols.size - 1)
ascii = symbols[im.astype(int)]
letter_size = font.getsize("x") im_out_size = new_im_size * letter_size bg_color = "black" im_out = Image.new("RGB", tuple(im_out_size), bg_color) draw = ImageDraw.Draw(im_out)
y = 0 for i, line in enumerate(ascii): for j, ch in enumerate(line): color = tuple(im_color[i, j]) draw.text((letter_size[0] * j, y), ch[0], fill=color, font=font) y += letter_size[1]
im_out.save(file[0:10] + ".jpg")
if __name__ == "__main__": for i in range(47, 60): file = "./img/{:0>4d}.jpg".format(i) ascii_art(file)
|