Skip to content

Instantly share code, notes, and snippets.

@eni23
Created July 8, 2020 20:17
Show Gist options
  • Save eni23/073960297a4e7d03538bb3bca422f944 to your computer and use it in GitHub Desktop.
Save eni23/073960297a4e7d03538bb3bca422f944 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import math
import shutil
from PIL import Image
def doublepixel(c1, c2):
return f"\x1b[48;2;{c2[0]};{c2[1]};{c2[2]}m\x1b[38;2;{c1[0]};{c1[1]};{c1[2]}m▀\x1b[0m"
def pixel(c):
return f"\x1b[38;2;{c[0]};{c[1]};{c[2]}m██\x1b[0m"
def main():
try:
img = Image.open(sys.argv[1]).convert("RGB")
except:
print("cannot open image")
return 1
cols, rows = shutil.get_terminal_size()
rows = int((rows-1)/2)
osize = img.size
basewidth = cols
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
pix = img.load()
out = ""
for v in range(0, img.height, 2):
for h in range(0, img.width):
px = list(pix[h,v])
try:
px1 = list(pix[h,v+1])
except:
px1=[0,0,0]
out += doublepixel(px, px1)
out+="\n"
print(out)
print(f"cols={cols} rows={rows} img_o={osize[0]}x{osize[1]} img_rsc={img.width}x{img.height}, ratio={wpercent}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment