利用 PIL
庫來獲取圖片并修改大小,
利用灰度值轉(zhuǎn)換公式把每一個(gè)像素的 RGB
值轉(zhuǎn)為灰度值
gray = int(0.2126*r+0.7152*g+0.0722*b)
再從字符集里獲取對(duì)應(yīng)的字符
asciis = list('M%$@#WNBRwm8S5A4E3KXFPH69nsxeazgpqbdoctfhkyvuGZYVTUCI2QOD0L7Jjl1ri!^{}[]()/|;:*>_~-,. ')
最后將字符連接起來并保存就完成了
在這個(gè)工程中,我們需要的第三方庫是 PIL
庫
但我們不用 pip install PIL
來安裝它,而是使用 pip install pillow
pip install pillow
導(dǎo)入庫
在導(dǎo)入 PIL
庫時(shí),不能用 import pillow
,應(yīng)使用 import PIL
from PIL import Image as Image
inputfile = input('inputfile:') outputfile = input('outputfile:') distance = {'y':' ','':' ','n':''} distance = distance[input('distance?(Y/n):')] re = input("resize?:")
字母占用的位置是矩形的,因此生成出來的字符畫會(huì)被“擠壓”。我們可以在字母與字母之間添加空格來防止這種情況的發(fā)生。
如果圖片太大了,會(huì)導(dǎo)致耗費(fèi)時(shí)間過長(zhǎng)、亂碼等問題。我們應(yīng)該對(duì)圖片進(jìn)行必要的縮放。在詢問“resize?”時(shí),可以設(shè)置以下幾種回答:
回答方式 | 作用 |
---|---|
“”,啥也不輸入 | 不縮放 |
“100”,邊長(zhǎng) | 輸入單個(gè)數(shù)字時(shí),會(huì)按比例縮放為較長(zhǎng)邊為此長(zhǎng)度的矩形 |
“100,200”,寬和高 | 縮放為指定寬高的矩形 |
使用 PIL
的 open
函數(shù)打開圖片
image = Image.open(inputfile)
注意:這里的 open 函數(shù)不要和 python 內(nèi)置函數(shù) open 混淆
獲取圖片大小
w, h = image.size
獲取變量 re
中存儲(chǔ)的大小信息,并用函數(shù) split
分割
nwh = re.split(',') for i in range(len(nwh)): nwh[i] = int(nwh[i])
調(diào)整圖片大小
if len(nwh) == 1: #如果項(xiàng)數(shù)為1,表示用戶只輸入了一個(gè)數(shù)字。即按比例縮放為較長(zhǎng)邊為此長(zhǎng)度的矩形 ww = int(nwh[0] / max(w,h) * w) #max函數(shù)獲取較大值 hh = int(nwh[0] / max(w,h) * h) image = image.resize((ww,hh),Image.ANTIALIAS) #改變圖片大小 #第一個(gè)參數(shù)放入一個(gè)元組,指定寬高 #第二個(gè)參數(shù) Image.ANTIALIAS 表示獲取高質(zhì)量圖片 else: #項(xiàng)數(shù)不為1,縮放為指定寬高的矩形 image = image.resize((nwh[0],nwh[1]),Image.ANTIALIAS)
指定轉(zhuǎn)換的字符集
asciis = list('M%$@#WNBRwm8S5A4E3KXFPH69nsxeazgpqbdoctfhkyvuGZYVTUCI2QOD0L7Jjl1ri!^{}[]()/|;:*>_~-,. ') #list函數(shù)將字符串轉(zhuǎn)換為列表
定義轉(zhuǎn)換字符的函數(shù)
def getasc(r,g,b,t=100): #t為透明度 if t == 0: return(' ') #如果是透明的,則直接返回空值 else: asc = '' gray = int(0.2126*r+0.7152*g+0.0722*b) #轉(zhuǎn)灰度值 asc = asciis[int(len(asciis)/256*(gray))] #獲取字符 return(asc)
開始轉(zhuǎn)換字符
for i in range(h): for o in range(w): #按行讀取每一個(gè)像素的RGB值 p = image.getpixel((o,i)) g = getasc(*p) # * 將參數(shù)列表轉(zhuǎn)換為多個(gè)項(xiàng) txt = txt + g + distance #連接字符 txt = txt + '\n' #換行
函數(shù) getpixel
獲取指定位置的 RGB
值,它的第一個(gè)參數(shù)為元組,傳入像素位置 (x,y)
,如果圖片是 JPG
格式的,它會(huì)返回含三項(xiàng)的列表 [r,g,b]
,如果圖片是 PNG
格式的,它會(huì)返回含四項(xiàng)的列表 [r,g,b,t]
,t
是透明度
使用 python
內(nèi)置函數(shù) open
保存文件
with open(outputfile,'w') as f: # 'w' 表示寫入 f.write(txt)
================== RESTART: D:\Python38-32\Files\ji2a\ji2a.py ==================
=====image to ascii=====
inputfile:
dora.png
outputfile:
dora.txt
distance?(Y/n):
y
resize?(needn't:'', square:side length, restangle:width,height):
100Opening 'dora.png'...
Getting...
Saving...
Seccessfully
原圖:
結(jié)果:
from PIL import Image as Image asciis = list('M%$@#WNBRwm8S5A4E3KXFPH69nsxeazgpqbdoctfhkyvuGZYVTUCI2QOD0L7Jjl1ri!^{}[]()/|;:*>_~-,. ') #gray = int(0.2126*r+0.7152*g+0.0722*b) def main(): global asciis print('=====image to ascii=====') inputfile, outputfile, distance, re = getargs() image = openfile(inputfile) image = resize(image,re) w, h = image.size txt = gettxt(image,w,h,distance) savefile(outputfile,txt) print('Seccessfully') def getargs(): inputfile = input('inputfile:\n') outputfile = input('outputfile:\n') distance = {'':' ','y':' ','n':''} distance = distance[input('distance?(Y/n):\n')] re = input("resize?(needn't:'', square:side length, restangle:width,height):\n") return(inputfile,outputfile,distance,re) def openfile(inputfile): print("\nOpening '"+inputfile+"'...") image = Image.open(inputfile) return(image) def resize(image,re): if re != '': print('Resizing...') nwh = re.split(',') for i in range(len(nwh)):nwh[i]=int(nwh[i]) w, h = image.size if len(nwh) == 1: ww = int(nwh[0] / max(w,h) * w) hh = int(nwh[0] / max(w,h) * h) image = image.resize((ww,hh),Image.ANTIALIAS) else: image = image.resize((nwh[0],nwh[1]),Image.ANTIALIAS) return(image) def gettxt(image,w,h,distance): txt = '' print('Getting...') for i in range(h): for o in range(w): p = image.getpixel((o,i)) txt = txt + getasc(*p) + distance txt = txt + '\n' return(txt) def getasc(r,g,b,t=100): if t == 0: return(' ') else: asc = '' gray = int(0.2126*r+0.7152*g+0.0722*b) asc = asciis[int(len(asciis)/256*(gray))] return(asc) def savefile(outputfile,txt): print('Saving...') with open(outputfile,'w') as f: f.write(txt) return() if __name__ == '__main__': main()
此代碼在 Python3.8 下調(diào)試通過
我們的圖片轉(zhuǎn)字符畫程序完成了!
要想將它打造成一個(gè)真正的命令行工具,可以加入命令行參數(shù)功能,
利用 sys
模塊的 argv
函數(shù)獲取命令行參數(shù),
利用 getopt
模塊的 getop
函數(shù)解析命令行參數(shù)。
到此這篇關(guān)于Python簡(jiǎn)單實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的實(shí)例項(xiàng)目的文章就介紹到這了,更多相關(guān)Python 圖片轉(zhuǎn)字符畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:金華 貴州 赤峰 陽泉 日照 雙鴨山 臨汾 克拉瑪依
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python簡(jiǎn)單實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的實(shí)例項(xiàng)目》,本文關(guān)鍵詞 Python,簡(jiǎn)單,實(shí)現(xiàn),圖片,轉(zhuǎn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。