• 没有对象?程序员的浪漫,对象攻略(1)


    废话少说,直接上图:

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述


    理论:

    对图片灰度值以及字符的灰度值都做histogram,并进行主要部分的对应,可以改善对比度和显示效果

    经过我在网上拼拼凑凑,以及自己稍微改改得到的代码如下:


    PIL库:

    只需要安装pillow库就行了。以下是终端安装代码:

    pip install pillow
    
    • 1

    完整源码:

    from PIL import Image, ImageDraw, ImageFont
    import operator, bisect
    def getChar(val):
        index = bisect.bisect_left(scores, val)
        if index > 0 and sorted_weights[index][1] + sorted_weights[index -
                                                                   1][1] > 2 * val:
            index -= 1
        return sorted_weights[index][0]
    
    def transform(image_file):
        image_file = image_file.convert("L")
        codePic = ''
        for h in range(image_file.size[1]):
            for w in range(image_file.size[0]):
                gray = image_file.getpixel((w, h))
                codePic += getChar(maximum * (1 - gray / 255))
            codePic += '
    '
        return codePic
    
    
    PictureName = input("请输入您要转换的照片名称:")
    readinFilePath = f'{PictureName}'
    outputTextFile = f'{PictureName}_ascii.txt'
    outputImageFile = f'{PictureName}_ascii.jpg'
    fnt = ImageFont.truetype('Courier New.ttf', 10)
    chrx, chry = fnt.getsize(chr(32))
    normalization = chrx * chry * 255
    weights = {}
    
    for i in range(32, 127):
        chrImage = fnt.getmask(chr(i))
        sizex, sizey = chrImage.size
        ctr = sum(
            chrImage.getpixel((x, y)) for y in range(sizey) for x in range(sizex))
        weights[chr(i)] = ctr / normalization
    weights[chr(32)] = 0.01
    weights.pop('_', None)
    weights.pop('-', None)
    sorted_weights = sorted(weights.items(), key=operator.itemgetter(1))
    scores = [y for (x, y) in sorted_weights]
    maximum = scores[-1]
    base = Image.open(open(readinFilePath, 'rb'))
    resolution = 0.3
    sizes = [resolution * i for i in (0.665, 0.3122, 4)]
    imagefile = base.resize((int(base.size[0] * sizes[0]),
                             int(base.size[1] * sizes[1])))
    result = transform(imagefile)
    
    asc_text = open(outputTextFile, 'w')
    asc_text.write(result)
    asc_text.close()
    
    asc_image = Image.new(
        'L', (int(base.size[0] * sizes[2]), int(base.size[1] * sizes[2])), 255)
    d = ImageDraw.Draw(asc_image)
    d.text((0, 0), result, font=fnt, fill=0)
    asc_image.save(outputImageFile)
    asc_image.show()
    asc_image.close()
    

    补充说明:

    1. 我使用了Courier New这个字体,它是等宽字体,我针对每一个character估算了一下它的灰度,并排序,将图片0~255的灰度映射到字符[32, 126]上面。
    2. Courier New如果没有的话,可以从trishume/OpenTuringCompiler下载。也可以下方评论区留言。
    3. resolution是精细程度,小则粗糙,大则精细,可以大于1,必须大于0。
    4. 我专门删除了下划线和破折号,因为这两种符号的指向性太强,容易影响图片整体观感。
    5. 特殊参数sizes中的(0.665, 0.3122, 4)是我根据Courier New字体,字体大小10,以及PIL ImageFont输出情况调整出来的,不同字体需要微调这其中的比例。

    使用说明:

      1. 复制上述所有源码。
      2. 将需要字符画的图片(照片)放置当前文件夹下。
      3. resolution是精细程度,小则粗糙,大则精细,可以大于1,必须大于0。
      4. 下载Courier New字体,添加至当前文件夹下。(没有此字体的可以私聊,发你)
      5. Run运行,直接在控制台打印图片(照片)的全名称,回车即可。
      6. 最后得到转换后的照片,和一个txt文本文档。
        在这里插入图片描述
        在这里插入图片描述本文首发于python黑洞网,博客园同步更新
  • 相关阅读:
    Springboot开发微信公众号(四)
    Springboot开发微信公众号(三)
    springboot中Scheduled不执行的原因
    static方法里用@Autowire或者@Resource注入的属性
    Spring boot 读取配置文件application.yml (自定义属性值)
    Apache-Flink深度解析-SQL概览
    Apache-Flink深度解析-DataStream-Connectors之Kafka
    Apache-Flink深度解析-JOIN 算子
    Apache-Flink深度解析-TableAPI
    Spark streaming消费Kafka的正确姿势
  • 原文地址:https://www.cnblogs.com/pythonzhilian/p/13662725.html
Copyright © 2020-2023  润新知