Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 114 additions & 68 deletions ASCII ART.ipynb

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions convertToASCII.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import division
from IPython.html.widgets import interactive

from imageUtils import show_image, show_images, get_image_from_url, resize_image, convert_image_to_grayscale, get_image_pixel_data
from listUtils import join_list_items, add_to_list, reshape_list

ASCII_A = [ '..', '%%', '@@', '??', 'SS', '++', '..', '**', '::', ',,', '..']
ASCII_B = [ '##', '??', '%%', '..', 'SS', '++', '..', '**', '::', ',,', '@@']
ASCII_C = [ '##', '??', '%%', '..', 'SS', '++', '..', '**', '::', ',,', '..']
ASCII_D = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
ASCII_E = ["$$","@@","BB","%%","88","&&","WW","MM","##","**","oo","aa","hh","kk","bb","dd","pp","qq","ww","mm","ZZ","OO","00","QQ","LL","CC","JJ","UU","YY","XX","zz","cc","vv","uu","nn","xx","rr","jj","ff","tt","//","\\","||","((","))","11","{{","}}","[[","]]","??","--","__","++","~~","<<",">>","ii","!!","ll","II",";;","::",",,",'""',"^^","``","''",".."," "]

def get_ascii_for_pixel(pixel_value, ascii_chars):
range_width = 256/(len(ascii_chars)-1)
ascii_index = int(pixel_value/range_width)

return ascii_chars[ascii_index]

def make_to_ascii(image_url_path, ascii_chars, new_width=35):
# First, get image from URL.
image = get_image_from_url(image_url_path)

# Next, resize image to new width while preserving aspect ratio.
(original_width, original_height) = image.size
aspect_ratio = original_height/original_width
new_height = int(aspect_ratio * new_width)
small_image = resize_image(image, (new_width, new_height))

# Then, make the image grayscale.
gray_image = convert_image_to_grayscale(small_image)

# Let's use the pixel values to map to the ASCII character set of choice.
pixels_in_image = list(get_image_pixel_data(gray_image))
pixels_to_chars = [get_ascii_for_pixel(pixel_value, ascii_chars) for pixel_value in pixels_in_image]

# Shape the ASCII list to make the final ASCII image.
ascii_width = new_width * len(ascii_chars[0])
ascii_art = reshape_list(pixels_to_chars, ascii_width)

show_images([image, small_image, gray_image])
print(ascii_art)

return ascii_art

convert_image_to_ascii = interactive(make_to_ascii, image_url_path='http://png.clipart.me/previews/a03/puppy-vector-8-39942.jpg', ascii_chars = {'a': ASCII_A, 'b': ASCII_B, 'c': ASCII_C, 'd': ASCII_D, 'e': ASCII_E}, new_width=[20, 50, 5])
13 changes: 13 additions & 0 deletions imageUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def show_image(image):

plt.show()


def show_images(images):
num_images = len(images)
fig, axes = plt.subplots(ncols=num_images)
plt.figure(figsize=(3,9))
plt.gray()

for i in range(num_images):
axes[i].imshow(images[i])
axes[i].axis('off')

plt.show()

# This function grabs the image from a url so we can change it.
def get_image_from_url(image_url_path):
file = cStringIO.StringIO(urllib.urlopen(image_url_path).read())
Expand Down