Last active
July 18, 2021 12:42
-
-
Save Frank-Buss/f7f91d5d04d47a2fe70bf8c22a5acaad to your computer and use it in GitHub Desktop.
Converts a PNG image to an echo command with ANSI codes. Example: https://i.imgur.com/Kw6LUbG.png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Converts a PNG image to an echo command with ANSI codes | |
# Copyright (c) 2020, Frank Buss | |
# All rights reserved. | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# 1. Redistributions of source code must retain the above copyright notice, this | |
# list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
from PIL import Image | |
import numpy as np | |
import math | |
import sys | |
import argparse | |
# parse arguments | |
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, | |
description='Converts a PNG file to an echo command with ANSI codes.\n\n' | |
'Examples:\n\n' | |
'png2ansi.py image.png\n' | |
'Read the PNG image image.png and outputs the echo command.\n\n') | |
parser.add_argument('input', help='the PNG input file name') | |
args = parser.parse_args() | |
# load image | |
im = Image.open(args.input) | |
im = im.convert('RGBA') | |
p = np.array(im) | |
# convert to ANSI sequence | |
for y in range(im.height): | |
sys.stdout.write('echo -e "') | |
for x in range(im.width): | |
# get pixel color | |
r, g, b, a = p[y][x] | |
# write echo code | |
sys.stdout.write("\\e[48;2;%d;%d;%dm" % (r, g, b)) | |
sys.stdout.write(" ") | |
sys.stdout.write('\\e[0m"\n') | |
sys.stdout.write('echo "png2ansi converter by Frank Buss"\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment