Created
December 21, 2012 05:10
-
-
Save bobmurder/4350792 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import os | |
import sys | |
from math import ceil, floor | |
from PIL import Image | |
RATIO = (1/2.35) | |
SYMBOL_STRING = '&#.:*"..' | |
EIGHT_COLOR = [(0, 0, 0), # 0 | |
(0, 0, 255), # 1 | |
(0, 255, 0), # 2 | |
(0, 255, 255), # 3 | |
(255, 0, 0), # 4 | |
(255, 0, 255), # 5 | |
(255, 255, 0), # 6 | |
(255, 255, 255)] # 7 | |
lookup_table = {k: v for k, v in zip(EIGHT_COLOR, SYMBOL_STRING)} | |
def three_bit(n): | |
''' Convert an image to 3 bit color ''' | |
return 0 if n <= 128 else 255 | |
def pixel_data(image_path, converter=three_bit, shrink=3.5): | |
''' Returns a list containing rows of pixel data ''' | |
im = Image.open(image_path) | |
height, width = im.size | |
# resize | |
small = im.resize((int(floor(height/shrink)), int(floor((width/shrink) * RATIO)))) | |
# convert to n bit color | |
small = small.point(converter) | |
# load the pixel table for the small n bit image | |
pixels = small.load() | |
s_height, s_width = small.size | |
return [[lookup_table[pixels[x, y]] for x in xrange(s_height)] for y in xrange(s_width)] | |
if __name__ == '__main__': | |
image_path = sys.argv[1] | |
data = pixel_data(image_path) | |
for row in data: | |
print ''.join(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment