Skip to content

Instantly share code, notes, and snippets.

@TransparentLC
Created May 17, 2023 09:20
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 TransparentLC/4a1a27a3f54cafd04b9cc892760e4030 to your computer and use it in GitHub Desktop.
Save TransparentLC/4a1a27a3f54cafd04b9cc892760e4030 to your computer and use it in GitHub Desktop.
import sys
from PIL import Image
if len(sys.argv) < 2:
print(f'Usage: python3 {sys.argv[0]} /path/to/image [/path/to/output]')
sys.exit(1)
f = sys.stdout if len(sys.argv) < 3 else open(sys.argv[2], 'w', encoding='utf-8')
literal = False
with Image.open(sys.argv[1]) as img:
img = img.convert('RGBA')
for y in range(0, img.size[1], 2):
for x in range(img.size[0]):
colorTop = img.getpixel((x, y))
if colorTop[3] < 127:
colorTop = None
else:
colorTop = colorTop[:3]
if y + 1 < img.size[1]:
colorBottom = img.getpixel((x, y + 1))
if colorBottom[3] < 127:
colorBottom = None
else:
colorBottom = colorBottom[:3]
else:
colorBottom = None
if colorTop and colorBottom:
output = '\x1b[38;2;{};{};{}m\x1b[48;2;{};{};{}m▀'.format(*colorTop[:3], *colorBottom[:3])
elif colorTop:
output = '\x1b[38;2;{};{};{}m\x1b[49m▀'.format(*colorTop[:3])
elif colorBottom:
output = '\x1b[38;2;{};{};{}m\x1b[49m▄'.format(*colorBottom[:3])
else:
output = '\x1b[0m '
if literal:
output = output.replace('\x1b', '\\x1b')
f.write(output)
f.write('\\x1b[0m\\n' if literal else '\x1b[0m\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment