python用字符組成圖像代碼實(shí)例
一、特效預(yù)覽
處理前
處理后
細(xì)節(jié)放大后
二、程序原理
將圖片所在的 256 的灰度映射到相應(yīng)的字符上面
也就是 RGB 值轉(zhuǎn)成相應(yīng)的字符
然后再將字符其寫(xiě)入文件即可
你聽(tī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)文章
python使用opencv換照片底色的實(shí)現(xiàn)
這篇文章主要介紹了python使用opencv換照片底色的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11python實(shí)現(xiàn)將文本轉(zhuǎn)換成語(yǔ)音的方法
這篇文章主要介紹了python實(shí)現(xiàn)將文本轉(zhuǎn)換成語(yǔ)音的方法,涉及Python中pyTTS模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05python生成13位或16位時(shí)間戳以及反向解析時(shí)間戳的實(shí)例
這篇文章主要介紹了python生成13位或16位時(shí)間戳以及反向解析時(shí)間戳的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03將 Ubuntu 16 和 18 上的 python 升級(jí)到最新 python3.8 的方法教程
這篇文章主要介紹了如何將 Ubuntu 16 和 18 上的 python 升級(jí)到最新 3.8 版,本文通過(guò)實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Python BeautifulSoup [解決方法] TypeError: list indices must be
這篇文章主要介紹了Python BeautifulSoup [解決方法] TypeError: list indices must be integers or slices, not str,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果
今天教大家怎么用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05如何導(dǎo)出python安裝的所有模塊名稱(chēng)和版本號(hào)到文件中
Python 模塊(Module),是一個(gè) Python 文件,以 .py 結(jié)尾,包含了 Python 對(duì)象定義和Python語(yǔ)句。這篇文章主要介紹了如何導(dǎo)出python安裝的所有模塊名稱(chēng)和版本號(hào)到文件中,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-06-06python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物商城
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單購(gòu)物商城的相關(guān)資料,具有實(shí)用性和一定的參考價(jià)值,感興趣的朋友可以參考一下2016-05-05