老規矩,先上效果圖


理論很簡單,就是對圖片操作,這里需要用到PIL的python包,里面有很好用的影像處理功能,
先打開圖片,把影像調整大小,
img = Image.open(picPath)
img = img.resize((picW, picH))
然后讀取灰度值,再把灰度值和字符對應起來就行,
from PIL import Image
lstChars = list("$@B%8&WM#*oahkbdpqwmZO0QLaCJUYXzczjhdhsdavunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'.")
def oneChars(r, g, b, alpha = 256):
global lstChars
length = len(lstChars)
gray = int(0.2126 * r + 0.7152 * g + 0.722 * b)
index =length*gray
return lstChars[index]
picPath = "C:\Users\Administrator\Desktop\\aaaaa\\aa.png"
picH = 40
picW = 80
img = Image.open(picPath)
img = img.resize((picW, picH))
txt = ""
for y in range(picH):
for x in range(picW):
txt += oneChars(img.getpixel((x, y)))
txt += '\n'
print txt
有什么想法,歡迎留言交流,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/277033.html
標籤:python
上一篇:matlab圖形繪制(一)
下一篇:Python基礎復習07_函式
