Last active
March 14, 2022 17:08
-
-
Save hungneox/45ef2df9e8e09189af53416ebd4d3f66 to your computer and use it in GitHub Desktop.
Image to ASCII art [Python]
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
from PIL import Image | |
img = Image.open("star.jpg") | |
pixels = img.rotate(90).load() | |
density = "Ñ@#W$9876543210?!abc;:+=-,._ " | |
for i in range(img.size[0]): | |
for j in range(img.size[1]): | |
r, b, g = pixels[i, j] | |
avg = int(round((r + b + g) / 3)) | |
length = 28 # len(density) | |
percent = avg / 255 | |
char_index = round((1 - percent) * length) | |
print(density[char_index] + " ", end="") | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment