基于python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)代碼實(shí)例
直接上代碼圖片就使用我家爽妹子的吧
如果沒(méi)有安裝pil模塊的話先cmd安裝下
輸入:pip install pillow
# -*- coding: utf-8 -*-from PIL import ImagecodeLib = ’’’@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,'^`’. ’’’#生成字符畫(huà)所需的字符集count = len(codeLib)def transform1(image_file): image_file = image_file.convert('L')#轉(zhuǎn)換為黑白圖片,參數(shù)'L'表示黑白模式 codePic = ’’ for h in range(0,image_file.size[1]): #size屬性表示圖片的分辨率,’0’為橫向大小,’1’為縱向 for w in range(0,image_file.size[0]): gray = image_file.getpixel((w,h)) #返回指定位置的像素,如果所打開(kāi)的圖像是多層次的圖片,那這個(gè)方法就返回一個(gè)元組 codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度與字符集的映射 codePic = codePic+’rn’ return codePicdef transform2(image_file): codePic = ’’ for h in range(0,image_file.size[1]): for w in range(0,image_file.size[0]): g,r,b = image_file.getpixel((w,h)) gray = int(r* 0.299+g* 0.587+b* 0.114) codePic = codePic + codeLib[int(((count-1)*gray)/256)] codePic = codePic+’rn’ return codePicfp = open(r’C:路徑3.jpg’,’rb’)image_file = Image.open(fp)image_file=image_file.resize((int(image_file.size[0]*0.2), int(image_file.size[1]*0.1)))#調(diào)整圖片大小print (u’Info:’,image_file.size[0],’ ’,image_file.size[1],’ ’,count)tmp = open(’tmp.txt’,’w’)tmp.write(transform1(image_file))tmp.close()a,b,c=1,2,3print(a,b,c)
結(jié)果不知還能否看出來(lái)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML在語(yǔ)音合成中的應(yīng)用2. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)3. 不要在HTML中濫用div4. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)5. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))6. XML入門(mén)的常見(jiàn)問(wèn)題(三)7. jscript與vbscript 操作XML元素屬性的代碼8. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別9. .NET Core 分布式任務(wù)調(diào)度ScheduleMaster詳解10. XML 非法字符(轉(zhuǎn)義字符)
