ASCII Art Example Using a Jupyter Notebook


In [8]:
from PIL import Image

ASCII_CHARS_RAW = "#@%x*=+;:. " 
ASCII_CHARS = list(ASCII_CHARS_RAW)

def scale_image(image, new_width=100):
    """Resizes an image preserving the aspect ratio.
    """
    (original_width, original_height) = image.size
    aspect_ratio = (original_height/float(original_width))*0.5
    new_height = int(aspect_ratio * new_width)

    new_image = image.resize((new_width, new_height))
    return new_image

def convert_to_grayscale(image):
    return image.convert('L')

def map_pixels_to_ascii_chars(image, range_width=25):
    """Maps each pixel to an ascii char based on the range
    in which it lies.

    0-255 is divided into 11 ranges of 25 pixels each.
    """

    pixels_in_image = list(image.getdata())

    pixels_to_chars = [ASCII_CHARS[pixel_value/range_width] for pixel_value in
            pixels_in_image]

    return "".join(pixels_to_chars)

def convert_image_to_ascii(image, new_width=100):
    image = scale_image(image, new_width)
    image = convert_to_grayscale(image)

    pixels_to_chars = map_pixels_to_ascii_chars(image)
    len_pixels_to_chars = len(pixels_to_chars)

    image_ascii = [pixels_to_chars[index: index + new_width] for index in
            xrange(0, len_pixels_to_chars, new_width)]

    return "\n".join(image_ascii)

def handle_image_conversion(image_filepath):
    image = None
    try:
        image = Image.open(image_filepath)
    except Exception, e:
        print "Unable to open image file {image_filepath}.".format(image_filepath=image_filepath)
        print e
        return

    image_ascii = convert_image_to_ascii(image)
    return image_ascii

image_file_path = "img/test_portrait2.jpg"
ascii_img = handle_image_conversion(image_file_path)

# Write it out to a text file
fh = open("img/test.txt", 'w')
fh.write(ascii_img)
fh.close()

# Print the result below
print(ascii_img)


*********xxxxxx%%%%%%%%%%%%%%%%%%%%@%%%%%%%%%%%xxx%%%%%%%%%xx********xxxxxx*******xxxxxxxxxxxxxx%%%%
xxxx**xxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%x*********xx%%xxx******xxxxxxxxxxxxxxx%%%%
xxxxxxxxxxx%%%%%%%%%%@@@%%%%%%%%%%%%%%%%%%%%%%x%%%%%%%%%%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%%%%xxxxxx***xxxxxxxxxxx****xxxx%%
%%%%%%%%%%%%%%%%xxxx%%+;;;:::;;;;;;;;;;;;;;;;;;;;;;;+++=+;*%%xxxxx%%%%%%xxxxxxxxxxx%%%%%%xxxxxxxxxx%
%%%%%%%%%%%%%%%%%xxx+;;;;+==++=+++++++=*x***==++++;;+++++==*xxxxx%%%%%%%%%%%%x%%%%%%%%%%%%xxxxxxxxx%
%%%%%%%%@@%%%@@@%%%%==;;+=*x%%x*=+;++*xxx%%@x*+++;;;+++++++=xxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%@@@@@@@@@@@@@@%%x=+;==;;+=**=+;;+=***===++;;;+++++;=*=+=*xxx%%%%%%%%%%@@@@@@@@@%%%%%%%@@@%%%%%%%
%%%%@@@@@@@@@@@@%%%%%x+;+;++++++;;;;++++;;;;++;+++++++;;=++*xxxx%%%%%%%%%%%%@@@@@@@@%%%%%@%%%%%%%%%%
%%%@@@@@@@@%%%@%%%%%%%+;;;;;++==;:::;;+*=+;;;;;;;;++;;;;;;=xxxxxxxxxxx%%%%%%%%@@@@@@@@%%%%%%%%%%%%%%
%%@@@@@@@%%%%%%%%%%%%%*;++++=**+;;+++++=***=++++++++;+;;;;xxxxxxxxxxxx%%%%%%%@@@@@%%%%%%%%%%%%%%%%%%
@@@@@@%%%%%%%%%%%%%%%%x+++==x*=***xx%xx***x**=+++++++++=+*xxx**xxxxx%%%%%%%%@@@@%%%%%%%@@%%%%%%%%@%%
@@@@@@@%%%@@@@%%%%%%%%%+++*xx******x******x%xx==++++++++*xxx*=**xxxxxx%%%%%%%%%%%%%%%%@@@%%%%%%%%%%%
@@@@@@@@@@@%%@@@@@%%%@%x+++=x*%@+;;;;;;+=x**x=+=++++++++=*******xxxxxxx%%%%%%%%%%%%%%%@@@@@@@@%%%%%%
@#####@@%%%%%%%%%%%%%%%%=+++=*++++==*=++====*=+=+++===+=;+****xxxxx%%x%%x%%%%%@%%%%%%%%%%@@@@@@%%%%%
######@@%%%%%%%%%%%xxxxxxx+===+;+++=====+=++=========+=++;*%**xxxxxxxx%%%%@@@@%%%%%%%%%%%@@@@@@%%%%%
######@@@@%%%%xx%%xxxxxxxxx*=+;;++++===++++++=====*===++++*x***xxx%%%%%%%%%@%%%xxxxxx%%%%%@@@@@@@@@@
#####@@@%%%%xxxxxxxxxxxxxxxxx*;;;;++++=++++++====*=*=+++++xx****x%%%%%%%%%%%%%xxxxxxx%%%%%%%@@@@@@%%
####@%%xx%%xx%xxxxxxxxxxxxxxxx*+;;+++===+;++=****x*=++++=*******=*x%%%%%xxxx%%xxxxxxxx%%%%%%%%%@%%%%
####@@%%%%%%%%%%%%xxxxxxx%%%%%=+==+===*===*****x**======x*******=*==*x%%%%%%%%%xxxxxxx%%%%%%%%%%%%%%
########@@@@@@@@%%%%xxxx%x*====+*====*******xx**======xx********x*=====*%%%%%%xxxxxx%%@@@%%%%%%xxxxx
################@@%%%x=++==+===+@x=++===**xx*=======*xxx*******%**========xx%xxxxx%%%%%%%%%%%xxxxxxx
#######@@@@@######%*+=========++=%*=++==**=========*x*x*******xx***=**======*x%%%%@@@@%%%%%%%%%%%%%x
#@@@@#@@@@@@@@##x===+=++====*=+;++%%**===========*xx*xxx****x%xx****==========*x%@@@@@@%%%%%%%%%%%%%
@@@@@@@@######%*==============;;;;=**+=+++++===**xx***x*****%**x*****===*=*======x@@@%%%%%%%%%%%%%%%
@%@@@@@@@@@@@%=*=============*;;;x#=+;;;;;+===**x*********x%****=****==============x@@@@@@@@%%%%%%%%
@@@@@@@@@@@%**==============*%=%%x%=;;;+==****************%x***=====================*%@@@@@@@@%%%%%%
@@@@@@@@@#@*x*===========*==%@x%==**++====*===*==****=***x%***=*======**==============x@@@@@@@%%%%%%
@@@@@@@@@@***========*======@%=****x%*=====+=****=*=***=x@x****=*==*================***=@@@@@@@@%%%%
####@@@@@x*x==============**xx===*xx@#=+:;+x******=*****%%**=*====*=================**===x@@@@@@@@%%
######@@x*x*===============%x*=**=%%%*=++=*===**====*==x%x=*========*========*===*****=**=x@@@@@@@@@
####@@@@**x*====*=========x%x*xx%@**x=++*+****==*=*=**@@xx=*====*==*============**xxxx*xxx**@@@@@@@@
###@@%%%****===========*=*%x*+x%xxxxx=+=====**=*=*=*%#%x*==*====*=======**==****xxxxxxxxxx*x*%@@@@@#
###@@%%%*x**==**=========*%x;+;*xxx%*;++===****=*=@@x*==========**====*====*xxx**xxxxxxxxxxxxx%@@@@@
###@@%%%*x****===========%xx;;++xxx%+*==*=====**==%x=*========*=+====*===**xxxxxxxxxxxxxx%xxxxx%@@@@
@@@@%%%x*x****=======**=x@x*;+++;*x*====*==****==**x====*=*========**=**xxxxxxxxxxx%x%%%%%xxxxxx###@
@%%%%%%%x*****====*=*===%%**;++++;%*==******==***=**x===========*=**=**x*****xx%%%xxxxxxxx%%%%xx@#@@
%%%%%%%xx*******=*====**@x*x=;;+++*+************=*@@x=====*=======*****xxxxx%%%xxxxxx*xx*xx%%%%xx@@@
@@@@@%%**x****==**=*****@****+;;+%===*****=*=**=x@x*==**==**=*=*****xxxx%xxxxxxxx*****xxxx***xxxxx@%
@@@@@%x*xx*************xx++*xx++*+*******x=****x%x=**=**==**==****x*xx%%xxx*xxxx*x***xxxxxx*xxxxxx%%

In [ ]: