Skip to content

Instantly share code, notes, and snippets.

@hungneox
Last active March 14, 2022 17:08
Show Gist options
  • Save hungneox/45ef2df9e8e09189af53416ebd4d3f66 to your computer and use it in GitHub Desktop.
Save hungneox/45ef2df9e8e09189af53416ebd4d3f66 to your computer and use it in GitHub Desktop.
Image to ASCII art [Python]
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