demo:

ascii art generator

input

output

program

cat.jpg

text

demo:

ascii art generator

How does this work??

First, we need to understand that all colors are just numbers

These are RGB sensors (aka "cones") in the human eye. We have a few million of these, which is equivalent to "megapixels"

Inspired by...

the human eye!!!

where else is this applied?

artificial intelligence:
"computer vision"
photobooth effects
SNAPCHAT!!!

functions in ascii-art.py

   scale_image(image, new_width=100)
    # resizes an image
   convert_to_grayscale(image)
    # converts image to grayscale
   map_pixels_to_ascii_chars(image, range_width=25)
    # maps each pixel to a character based on its  
      number range. 0-255 is divided into 11 ranges
      of 25 pixels each

   convert_image_to_ascii(image, new_width=100)
    # calls all of the functions above in a specific
      order, resulting in a string of characters

   handle_image_conversion(image_filepath)
    # calls convert_image_to_ascii (above) on the
      image file provided
import requests
from PIL import Image

ASCII_CHARS = [ '#', '?', '%', '.', 'S', '+', '.', '*', ':', ',', '@']

def scale_image(image, new_width=100):
    # resizes an image, slightly adjusting the aspect ratio.
    
    (original_width, original_height) = image.size
    aspect_ratio = (original_height/float(original_width)) * .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)
    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
            range(0, len_pixels_to_chars, new_width)]

    return "\n".join(image_ascii)

def handle_image_conversion(image_filepath):
    image = None
    try:
        response = requests.get(image_filepath)
        image = Image.open(BytesIO(response.content))
    except Exception as e:
        print("Unable to open image file {image_filepath}.".format(image_filepath=image_filepath))
        print(e)
        return

    image_ascii = convert_image_to_ascii(image)
    print(image_ascii)

image_file_path = "https://cdn.citylab.com/media/img/citylab/2018/02/RTR3MXS6/facebook.jpg?1518623597"
handle_image_conversion(image_file_path)

This is what I tell people who ask for advice about starting (or pivoting) their career in tech. “Be good at being bad at things,” I say. “You’ll have no idea what you’re doing, so you’ll just keep learning. You won’t get to be the best at everything or even most things or even some things. It’s not possible, and it’s not just you. So get good and comfortable having no idea, but figure out ways to get closer to having an idea.”

 

Gina Trapani, Partner & Director of Engineering at Postlight.
Founded ThinkUp, Makerbase, Todo.txt, Lifehacker.

Computer Vision

By Michelle Lim

Computer Vision

  • 984