Example #1
0
def URL_to_ASCII(url, width=80, height=24):
    # good bits from http://stevendkay.wordpress.com/2009/09/08/generating-ascii-art-from-photographs-in-python/
    # and http://blog.hardlycode.com/pil-image-from-url-2011-01/
    # TODO: MouseText
    greyscale = [
        " ", " ", ".,-", "_ivc=!/|\\~", "gjez2]/(YL)t[+T7Vf",
        "mdK4ZGbNDXY5P*Q", "W8KMA", "#%$"
    ]
    greyscale.reverse()  # white on black
    zonebounds = [36, 72, 108, 144, 180, 216, 252]
    img_file = urllib.urlopen(url)
    im = StringIO(img_file.read())
    im = Image.open(im)
    (w, h) = im.size
    # change width but not height
    new_width = int(width)
    new_height = int((float(width) / float(w)) * float(h))
    im = im.resize((new_width, new_height), Image.BILINEAR)
    im = im.convert("L")
    pic = ""
    for y in range(0, im.size[1]):
        for x in range(0, im.size[0]):
            lum = 255 - im.getpixel((x, y))
            row = bisect(zonebounds, lum)
            possibles = greyscale[row]
            pic = pic + possibles[random.randint(0, len(possibles) - 1)]
        pic = pic + "\n"
    return pic
Example #2
0
def URL_to_ASCII(url, width=80, height=24):
    # good bits from http://stevendkay.wordpress.com/2009/09/08/generating-ascii-art-from-photographs-in-python/
    # and http://blog.hardlycode.com/pil-image-from-url-2011-01/
    # TODO: MouseText
    greyscale = [" ", " ", ".,-", "_ivc=!/|\\~", "gjez2]/(YL)t[+T7Vf", "mdK4ZGbNDXY5P*Q", "W8KMA", "#%$"]
    greyscale.reverse()  # white on black
    zonebounds = [36, 72, 108, 144, 180, 216, 252]
    img_file = urllib.urlopen(url)
    im = StringIO(img_file.read())
    im = Image.open(im)
    (w, h) = im.size
    # change width but not height
    new_width = int(width)
    new_height = int((float(width) / float(w)) * float(h))
    im = im.resize((new_width, new_height), Image.BILINEAR)
    im = im.convert("L")
    pic = ""
    for y in range(0, im.size[1]):
        for x in range(0, im.size[0]):
            lum = 255 - im.getpixel((x, y))
            row = bisect(zonebounds, lum)
            possibles = greyscale[row]
            pic = pic + possibles[random.randint(0, len(possibles) - 1)]
        pic = pic + "\n"
    return pic