最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python用字符組成圖像代碼實(shí)例

 更新時(shí)間:2022年01月11日 09:25:39   作者:autofelix  
大家好,本篇文章主要講的是python用字符組成圖像代碼實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽

 一、特效預(yù)覽

處理前

處理后

細(xì)節(jié)放大后

二、程序原理

將圖片所在的 256 的灰度映射到相應(yīng)的字符上面

也就是 RGB 值轉(zhuǎn)成相應(yīng)的字符

然后再將字符其寫入文件即可

你聽懂了嗎 

三、程序源碼

#!/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 + '圖片轉(zhuǎn)換為字符圖片')
        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個(gè)字符上,也就是RGB值轉(zhuǎn)字符的函數(shù)
        :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()

總結(jié)

到此這篇關(guān)于python用字符組成圖像代碼實(shí)例的文章就介紹到這了,更多相關(guān)python字符組成圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

保山市| 合作市| 南溪县| 禄劝| 辽宁省| 宜黄县| 双城市| 襄樊市| 南华县| 红安县| 三明市| 兴城市| 崇左市| 三原县| 固始县| 阳新县| 郎溪县| 惠安县| 海城市| 上犹县| 颍上县| 苍南县| 南投市| 灵寿县| 湖北省| 太仓市| 兴宁市| 西华县| 北川| 丘北县| 平原县| 叶城县| 灯塔市| 连南| 康乐县| 翁牛特旗| 丰顺县| 崇文区| 高安市| 双鸭山市| 左权县|