佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > Python簡(jiǎn)單實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的實(shí)例項(xiàng)目

Python簡(jiǎn)單實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的實(shí)例項(xiàng)目

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 Win7旗艦版 客戶服務(wù) 語音系統(tǒng) 硅谷的囚徒呼叫中心 企業(yè)做大做強(qiáng) 電話運(yùn)營中心 百度AI接口

1. 原理

利用 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!^{}[]()/|;:*>_~-,. ')

最后將字符連接起來并保存就完成了

2. 開始制作

2.1 導(dǎo)入所需的庫

在這個(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

2.2 獲取圖片路徑和選項(xiàng)

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”,寬和高 縮放為指定寬高的矩形

2.3 圖片獲取

使用 PILopen 函數(shù)打開圖片

image = Image.open(inputfile)

注意:這里的 open 函數(shù)不要和 python 內(nèi)置函數(shù) open 混淆

2.4 調(diào)整圖片大小

獲取圖片大小

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)

2.5 轉(zhuǎn)換字符

指定轉(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 是透明度

2.6 保存文本

使用 python 內(nèi)置函數(shù) open 保存文件

with open(outputfile,'w') as f: # 'w' 表示寫入
    f.write(txt)

2.7 效果

================== 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):
100

Opening 'dora.png'...
Getting...
Saving...
Seccessfully

原圖:


結(jié)果:

3. 完整代碼

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)試通過

4. 后記

我們的圖片轉(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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的完整代碼
  • python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫
  • Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的示例(靜態(tài)圖片,gif皆可)
  • 基于python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫代碼實(shí)例
  • Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的代碼實(shí)例
  • 一百行python代碼將圖片轉(zhuǎn)成字符畫
  • Python將圖片轉(zhuǎn)換為字符畫的方法
  • python Opencv將圖片轉(zhuǎn)為字符畫
  • Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的示例
  • Python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的示例代碼
  • 從零學(xué)python系列之教你如何根據(jù)圖片生成字符畫

標(biāo)簽:海南 長(zhǎng)沙 崇左 喀什 山西 安康 山西 濟(jì)南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python簡(jiǎn)單實(shí)現(xiàn)圖片轉(zhuǎn)字符畫的實(shí)例項(xiàng)目》,本文關(guā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)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    柘荣县| 合水县| 无棣县| 灵武市| 弥渡县| 华宁县| 桂东县| 新野县| 樟树市| 景宁| 襄樊市| 五大连池市| 进贤县| 特克斯县| 汝城县| 南华县| 连江县| 会同县| 比如县| 应用必备| 崇文区| 河北区| 郎溪县| 霍邱县| 宜阳县| 洪泽县| 聊城市| 靖宇县| 丰镇市| 龙岩市| 博野县| 岗巴县| 济南市| 临潭县| 江口县| 张北县| 稻城县| 阜城县| 天台县| 张家港市| 博客|