Skip to content

Instantly share code, notes, and snippets.

@ligix
Created November 3, 2020 21:41
Show Gist options
  • Save ligix/081676b6bfd3671aaae1d98d2f91c84c to your computer and use it in GitHub Desktop.
Save ligix/081676b6bfd3671aaae1d98d2f91c84c to your computer and use it in GitHub Desktop.
Ascii art one liner
url="https://i.kym-cdn.com/photos/images/original/000/711/604/f6d.jpg";W=300;H=300;out="art.txt";asc="█@#O$%?o*+;:,. ";import requests;from PIL import Image;from io import BytesIO, StringIO;(a:=StringIO(),i:=Image.open(BytesIO(requests.get(url).content)),i:=i.convert("L"),w:=i.size[0],h:=i.size[1],r:=min(W/w,H/h),i:=i.resize((w:=int(w*r),h:=int(h*r))),[([a.write(asc[i.getpixel((x,y))//(255//len(asc))-1]) for x in range(w)], a.write("\n")) for y in range(h)]),open(out,"w").write(a.getvalue()) # url: url of the photo; W,H: maximum width / hieght of the ascii art; out: file where the ascii art will be saved; asc: characters, from the "darkest" to the "whitest". If you want to run it with python -c use single quotes ' to wrap the code, not double quotes " for obvious reasons
@ligix
Copy link
Author

ligix commented Nov 3, 2020

url: url of the photo
W,H: maximum width / hieght of the ascii art
out: file where the ascii art will be saved
asc: characters, from the "darkest" to the "whitest"

If you want to run it with python -c use single quotes ' to wrap the code, not double quotes " for obvious reasons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment