python特效之字符成像详解

作者:autofelix 时间:2021-06-07 02:18:45 

一、特效预览

python特效之字符成像详解

处理前

python特效之字符成像详解

处理后

  1. python特效之字符成像详解

细节放大后

二、程序原理

  • 将图片所在的 256 的灰度映射到相应的字符上面

  • 也就是 RGB 值转成相应的字符

  • 然后再将字符其写入文件即可

python特效之字符成像详解

你听懂了吗 

三、程序源码

#!/usr/bin/env python
# encoding: utf-8
from PIL import Image
class 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-07 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()

来源:https://blog.csdn.net/weixin_41635750/article/details/122416621

标签:python,特效,字符成像
0
投稿

猜你喜欢

  • python 2.7.14安装图文教程

    2023-09-22 16:08:33
  • 详细介绍Scrapy shell的使用教程

    2022-04-18 03:35:39
  • PyTorch实现重写/改写Dataset并载入Dataloader

    2023-10-31 17:19:35
  • 使用javascript+xml技术实现分页浏览

    2008-05-29 13:49:00
  • python异步Web框架sanic的实现

    2021-01-17 01:37:57
  • 提升Go语言开发效率的小技巧实例(GO语言语法糖)汇总

    2023-07-08 10:01:26
  • Python sklearn对文本数据进行特征化提取

    2023-05-19 09:07:04
  • 用JavaScript脚本实现的图灵机

    2009-01-21 18:10:00
  • SQL LOADER错误小结

    2024-01-20 23:26:36
  • Python中删除文件的几种方法实例

    2021-02-02 05:57:13
  • 使用pandas中的DataFrame数据绘制柱状图的方法

    2023-08-10 20:59:40
  • HTML+CSS 模仿Windows 7 桌面效果

    2010-06-17 14:33:00
  • python3.6数独问题的解决

    2022-06-21 20:40:32
  • JavaScrpt的面向对象全面解析

    2024-04-23 09:20:40
  • MYSQL大数据导入

    2024-01-18 18:09:36
  • Python中内置数据类型list,tuple,dict,set的区别和用法

    2022-09-10 05:59:46
  • Python爬虫获取op.gg英雄联盟英雄对位胜率的源码

    2024-01-02 13:03:52
  • Python实现扣除个人税后的工资计算器示例

    2021-06-02 14:34:39
  • Pytorch深度学习addmm()和addmm_()函数用法解析

    2021-01-02 04:04:25
  • mysql缓冲和缓存设置详解

    2024-01-27 02:39:18
  • asp之家 网络编程 m.aspxhome.com