Skip to content

Instantly share code, notes, and snippets.

@m4nh
Last active May 2, 2020 22:38
Show Gist options
  • Save m4nh/b73c193a98c12f980001d254691cf502 to your computer and use it in GitHub Desktop.
Save m4nh/b73c193a98c12f980001d254691cf502 to your computer and use it in GitHub Desktop.
#python function to print an #image within #console using special characters and colors
import sys, numpy as np, fire, sty
from PIL import Image
def print_image(filename, scale=0.1, ratio=7/4, character='█'):
"""
Args:
filename (Str): input image
scale (float): output scale. Default [0.1], means 48 cells width
ratio (float): image2cells ratio. Default [7/4]
character (char): character used to fill cells
"""
img = Image.open(filename).convert('RGB')
img.thumbnail([480,480])
S = (round(img.size[0]*scale*ratio), round(img.size[1]*scale) )
img = np.array(img.resize(S))
for i in range(img.shape[0]):
buf = []
for j in range(img.shape[1]):
sty.fg.col = sty.Style(sty.RgbFg(img[i,j,0], img[i,j,1], img[i,j,2]))
buf.append(sty.fg.col + str(character) + sty.fg.rs)
print(''.join(buf))
if __name__ == '__main__': fire.Fire(print_image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment