Skip to content

Instantly share code, notes, and snippets.

@shonenada
Last active January 4, 2016 14:19
Show Gist options
  • Save shonenada/8633628 to your computer and use it in GitHub Desktop.
Save shonenada/8633628 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
from PIL import Image
MAX_LEN = 100.0
FIX_RATE = 1.8
REPLACEMENT = 'MNHQ$OC?7>!:-;. '
def convert(filename):
image = Image.open(filename)
image = image.convert('L')
width, height = image.size
rate = MAX_LEN / width
width = int(rate * width)
height = int(rate * height / FIX_RATE)
image = image.resize((width, height))
data = image.load()
output = ''
for h in xrange(height):
for w in xrange(width):
output += REPLACEMENT[int(data[w, h] / 256.0 * 16)]
output += '\n'
print output
convert('demo.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment