python动态图片转字符画_使用python实现一个将图片转换成字符画的功能
使用python實現一個將圖片轉換成字符畫的功能
發布時間:2020-11-05 16:01:25
來源:億速云
閱讀:97
使用python實現一個將圖片轉換成字符畫的功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
直接上代碼圖片就使用我家爽妹子的吧
如果沒有安裝pil模塊的話先cmd安裝下
輸入:pip install pillow
# -*- coding: utf-8 -*-
from PIL import Image
codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字符畫所需的字符集
count = len(codeLib)
def transform1(image_file):
image_file = image_file.convert("L")#轉換為黑白圖片,參數"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)) #返回指定位置的像素,如果所打開的圖像是多層次的圖片,那這個方法就返回一個元組
codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度與字符集的映射
codePic = codePic+'\r\n'
return codePic
def 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+'\r\n'
return codePic
fp = 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)))#調整圖片大小
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,3
print(a,b,c)
結果不知還能否看出來
關于使用python實現一個將圖片轉換成字符畫的功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python动态图片转字符画_使用python实现一个将图片转换成字符画的功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小甲鱼python课后题答案_Pytho
- 下一篇: 法国aoc红酒与其他等级红酒的区别