Generate Ascii Images Like the Matrix

来源:互联网 发布:java找不到对象 编辑:程序博客网 时间:2024/06/18 14:30

Generate Ascii Image Like the Matrix

Inspired by this online image to text converter and this post on Oschina

script hosts at github. Poor hack, Any advice is welcomed.

Edit: Oooops, Demo sucks on my site, it’s perfect if you generate an alone html file.look here andhere

Demo





















































源码

#! /bin/env python# -*- coding: utf-8 -*-"""Turn images into acsii."""__author__ = 'Reverland (lhtlyy@gmail.com)'import Imageimport ImageOpsimport sysfilename = 'a.jpg'def makeHTMLbox(body, fontsize, imagesize):    """takes one long string of words and a width(px) then put them in an HTML box"""    boxStr = """<div style=\"font-size: %spx;line-height: 100%s; width: %s;background-color: rgb(0, 0, 0);border: 1px grey solid;text-align: center; overflow: hidden;\">%s</div>    """    return boxStr % (fontsize, '%', imagesize[0], body)def makeHTMLascii(body, color):    """take words and , and create an HTML word """    #num = str(random.randint(0,255))    # return random color for every tags    color = 'rgb(%s, %s, %s)' % color    # get the html data    wordStr = '<span style=\"color:%s;float:left;\">%s</span>'    return wordStr % (color, body)def i2m(im, fontsize):    """turn an image into ascii like matrix"""    im = im.convert('L')    im = ImageOps.autocontrast(im)    im.thumbnail((im.size[0] / fontsize, im.size[1] / fontsize))    string = ''    colors = [(0, i, 0) for i in range(0, 256, 17)]    words = '据说只有到了十五字才会有经验的'    for y in range(im.size[1]):        for x in range(im.size[0]):            p = im.getpixel((x, y))            i = 14            while i >= 0:                if p >= i * 17:                    s = makeHTMLascii(words[3 * i:3 * (i + 1)], colors[i])                    break                i -= 1            if x % im.size[0] == 0 and y > 0:                s = s + '<br/>'            string = string + s    return stringdef i2a(im, fontsize):    """turn an image into ascii with colors"""    im = im.convert('RGB')    im = ImageOps.autocontrast(im)    im.thumbnail((im.size[0] / fontsize, im.size[1] / fontsize))    string = ''    for y in range(im.size[1]):        for x in range(im.size[0]):            c = im.getpixel((x, y))            # print c            s = makeHTMLascii('翻', c)            if x % im.size[0] == 0 and y > 0:                s = s + '<br/>'            string = string + s    return stringdef getHTMLascii(filename, fontsize, style='matrix', outputfile='a.html', scale=1):    """Got html ascii image"""    im = Image.open(filename)    size = (int(im.size[0] * scale), int(im.size[1] * scale))    im.thumbnail(size, Image.ANTIALIAS)    if style == 'matrix':        ascii = makeHTMLbox(i2m(im, fontsize), fontsize, im.size)    elif style == 'ascii':        ascii = i2a(im, fontsize)    else:        print "Just support ascii and matrix now, fall back to matrix"        ascii = makeHTMLbox(i2m(im, fontsize), fontsize, im.size)    with open(outputfile, 'wb') as f:        f.write(ascii)    return 1if __name__ == '__main__':    if sys.argv[1] == '--help' or sys.argv[1] == '-h':        print """Usage:python i2a.py filename fontsize [optional-parameter]        optional-parameter:            scale -- between (0, 1)            style -- matrix or ascii"""    else:        filename = sys.argv[1]        try:            fontsize = int(sys.argv[2])        except:            fontsize = int(raw_input('input fontsize please:'))        try:            scale = float(sys.argv[3])        except:            scale = 1        try:            style = sys.argv[4]        except:            style = 'matrix'        getHTMLascii(filename, fontsize, scale=scale)

More Demo





























































原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一千多度的近视怎么办 近视镜片磨花了怎么办 眼镜镜片磨花了怎么办 戴眼镜鼻梁有印怎么办 狗狗发烧怎么办最有效 狗狗感冒怎么办最有效 狗狗发烧去医院怎么办 眼睛里长了虫子怎么办 吃了没熟的猪肉怎么办 没熟的鸡肉吃了怎么办 狗狗大便有绦虫怎么办 吃了有虫的猪肉怎么办 米猪肉吃了会怎么办 吃了鱼的寄生虫怎么办 鱼身体里有线虫怎么办 幼猫半夜一直叫怎么办 猫半夜4点叫不停怎么办 眼睛里长了黄斑怎么办 眼睛周围长小疙瘩怎么办 眼睛被手机砸了怎么办 眼睛被东西砸了怎么办 眼睛被打了一下怎么办 眼睛被树枝伤了怎么办 眼球被打了一下怎么办 眼睛黄有血丝是怎么办 上眼皮有异物感怎么办 20岁眼周围皱纹怎么办 才22岁眼睛皱纹怎么办 眼周围发干皱纹怎么办 眼周围淤血肿了怎么办 每到秋季就咳嗽怎么办 左肾泥沙样结石怎么办 双肾泥沙样结石怎么办 温州市民卡丢了怎么办 上眼皮过敏肿了怎么办 上眼皮又痒又肿怎么办 上眼皮红肿痛是怎么办 上眼皮肿的厉害怎么办 眼皮肿了还痒痒怎么办 眼睛被手指戳到怎么办 打球眼睛撞肿了怎么办