Last active
January 5, 2023 17:24
-
-
Save cdiener/10491632 to your computer and use it in GitHub Desktop.
Convert image to ascii art
This file contains 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
import sys; from PIL import Image; import numpy as np | |
chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@')) | |
if len(sys.argv) != 4: print( 'Usage: ./asciinator.py image scale factor' ); sys.exit() | |
f, SC, GCF, WCF = sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), 7/4 | |
img = Image.open(f) | |
S = ( round(img.size[0]*SC*WCF), round(img.size[1]*SC) ) | |
img = np.sum( np.asarray( img.resize(S) ), axis=2) | |
img -= img.min() | |
img = (1.0 - img/img.max())**GCF*(chars.size-1) | |
print( "\n".join( ("".join(r) for r in chars[img.astype(int)]) ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG Thank you for this!!!!!
I was able to use it with my GUI package and run it in a GUI window (tkinter based) and changed a line of code and ran it in a Browser (Remi based). And, it was trivial to add in your code. It was all done in 25 lines of code thanks your magic! Thank you!!!
Here's the PySimpleGUI one (tkinter)
Here's the PySimpleGUIWeb version
Sorry that I hacked up your code a bit, but it wasn't THAT bad.
Can I put the code here? I'll move it if asked. Just don't want to fart around with a Gist and it does show one way of integrating your code into something bigger perhaps