Skip to content

Instantly share code, notes, and snippets.

@patch-werk
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save patch-werk/9926905 to your computer and use it in GitHub Desktop.

Select an option

Save patch-werk/9926905 to your computer and use it in GitHub Desktop.
Takes a photo and coverts it to a ascii image. Requires numpy and PIL libraries.
import Image
import numpy as n
#open image and convert to greyscale
img = Image.open('photo.png').convert('LA')
#get dimensions and scale them
w,h = img.size
# change to 4 and 7, respectivly to make smaller image
w /=2
h /=4
#resize to smaller dimensions
img = img.resize([w,h])
#get a list of pixel data
data = list(img.getdata())
#basic ascii ramp, need to reverse it
chars = [' ','.',':','-','=','+','*','#','%','@']
charsrev = chars[::-1]
#create a file
f = open('ascimg.txt', 'w')
for y in range(h):
for x in range(w):
#get the current pixel from the list, scale its value and write to file
pixel = data[(y)*w+(x)]
ave = n.floor(pixel[0]/24)
f.write(charsrev[int(ave)])
f.write('\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment