Skip to content

Instantly share code, notes, and snippets.

@aessam
Created March 8, 2020 23:35
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 aessam/2958f0cefc7161a896219f0e364f983e to your computer and use it in GitHub Desktop.
Save aessam/2958f0cefc7161a896219f0e364f983e to your computer and use it in GitHub Desktop.
Not ASCII art.
from PIL import Image, ImageDraw
import sys
def rgb_2_pixels(fr, fg, fb, br, bg, bb, text='\u2584'):
fgc = "{0};{1};{2}".format(fr, fg, fb)
bgc = "{0};{1};{2}".format(br, bg, bb)
return "\033[38;2;{0};48;2;{1}m{2}\033[0m".format(fgc, bgc, text)
def morph_color(c1, c2):
return int((c1 + c2) / 2)
def morph_pixels(p1, p2):
return [
morph_color(p1[0], p2[0]),
morph_color(p1[1], p2[1]),
morph_color(p1[2], p2[2])
]
xstep = 4
ystep = 8
pix_in_char_table = {}
image_name = sys.argv[1]
output_name = image_name.split(".")[0] + "__image_2_ascii__.txt"
im = Image.open(image_name)
pix = im.load()
image_text = ""
for y in range(0, im.size[1] - ystep, ystep):
for x in range(0, im.size[0] - xstep, xstep):
c1 = pix[x, y]
c2 = pix[x, y + xstep]
for xx in range(x , x + xstep):
for yy in range(y, y + ystep):
if yy > y + (xstep):
c2 = morph_pixels(c2, pix[xx, yy])
else:
c1 = morph_pixels(c2, pix[xx, yy])
pass
image_text += rgb_2_pixels(c1[0], c1[1], c1[2], c2[0], c2[1], c2[2])
image_text += "\n"
print(image_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment