龙空技术网

Python又亮新招了!字符成像的特效,我相信没办法拒绝吧

WEN灬十二灬 1752

前言:

当前你们对“python文字特效”大体比较讲究,朋友们都需要了解一些“python文字特效”的相关资讯。那么小编同时在网摘上收集了一些对于“python文字特效””的相关内容,希望姐妹们能喜欢,朋友们一起来学习一下吧!

什么是 字符成像?

将原图片经过python处理后,生成完全由纯字符组成的图像。

一、特效预览

处理前↑

处理后↑

细节放大后↑

二、程序原理将图片所在的256的灰度映射到相应的字符上面也就是RGB值转成相应的字符然后再将字符其写入文件即可

三、程序源码

完整的源码整理好了,别忘了点赞、关注,私信【333】就好了!

#!/usr/bin/env python# encoding: utf-8from PIL import Imageclass charsetPicture:    '''     This is a main Class, the file contains all documents.     One document contains paragraphs that have several sentences     It loads the original file and converts the original file to new content     Then the new content will be saved by this class    '''    def __init__(self):        self.char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")        self.width = 160        self.height = 60    def hello(self):        '''        This is a welcome speech        :return: self        '''        print('*' * 50)        print(' ' * 18 + '图片转换为字符图片')        print(' ' * 5 + 'Author: autofelix  Date: 2022-01-19 13:14')        print('*' * 50)        return self    def get_char(self, r, g, b, alpha=256):        '''        将256灰度映射到70个字符上,也就是RGB值转字符的函数        :alpha: 透明度        :return: self        '''        if alpha == 0:            return ' '        length = len(self.char)        gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)        unit = (256.0 + 1) / length        return self.char[int(gray / unit)]    def run(self):        '''        The program entry        '''        im = Image.open('assets/aaa.jpeg')        im = im.resize((self.width, self.height), Image.NEAREST)        txt = ''        for i in range(self.height):            for j in range(self.width):                txt += self.get_char(*im.getpixel((j, i)))            txt += '\n'        print(txt)        with open('handler.txt', 'w') as f:            f.write(txt)if __name__ == '__main__':    charsetPicture().hello().run()

标签: #python文字特效