zoukankan      html  css  js  c++  java
  • 图片生成字符

    # -*- coding: utf-8 -*-
    
    from PIL import Image
    import numpy as np
    
    
    def print_photo(photo_file, width=120, k=1.0, reverse=False, outfile=None):
        """打印照片,默认120个字符宽度"""
    
        im = Image.open(photo_file).convert('L')  # 打开图片文件,转为灰度格式
        height = int(k * width * im.size[1] / im.size[0])  # 打印图像高度,k为矫正系数,用于矫正不同终端环境像素宽高比
        arr = np.array(im.resize((width, height)))  # 转为NumPy数组
        if reverse:  # 反色处理
            arr = 255 - arr
    
        chs = np.array([' ', '.', '-', '+', '=', '*', '#', '@'])  # 灰度-字符映射表
        arr = chs[(arr / 32).astype(np.uint8)]  # 灰度转为对应字符
    
        if outfile:
            with open(outfile, 'w') as fp:
                for row in arr.tolist():
                    fp.write(''.join(row))
                    fp.write('
    ')
        else:
            for i in range(arr.shape[0]):  # 逐像素打印
                for j in range(arr.shape[1]):
                    print(arr[i, j], end=' ')
                print()
    
    
    if __name__ == '__main__':
        print_photo('普天同庆.jpg', width=120, k=1.0, outfile='xufive.txt')

    来自:https://blog.csdn.net/xufive/article/details/103761569

  • 相关阅读:
    从迷宫终点出发——Leo鉴书36
    OCP-1Z0-053-V13.02-238题
    OCP-1Z0-053-V13.02-233题
    OCP-1Z0-053-V13.02-232题
    OCP-1Z0-053-V13.02-228题
    OCP-1Z0-053-V13.02-226题
    OCP-1Z0-053-V13.02-225题
    OCP-1Z0-053-V13.02-221题
    OCP-1Z0-053-V13.02-219题
    OCP-1Z0-053-V13.02-216题
  • 原文地址:https://www.cnblogs.com/gisoracle/p/12283180.html
Copyright © 2011-2022 走看看