记录 python图片转字符画
使用盲文字符
效果图:
from PIL import Image
imgSrc = 'test.jpg'
chars = list("⣿⣷⣾⢿⡾⣮⣭⣝⣫⣥⡛⣏⡏⢸⡇⢘⠛⡲⠲⢃⠒⠤⣀⠄") # 使用的字符
output = '1.txt'
width = 512
height = 512
def getChar(r, g, b, a=256):
if a == 0:
return '⠄'
length = len(chars)
gray = 0.2126 * r + 0.7152 * g + 0.0722 * b # 灰度计算
block = (256.0 + 1) / length
return chars[int(gray / block)]
txt = ''
img = Image.open(imgSrc)
img = img.resize((width, height), Image.NEAREST)
imgWidth = img.width
imgHeight = img.height
for i in range(imgHeight):
for j in range(imgWidth):
txt += getChar(*img.getpixel((j, i)))
txt += '\n'
img.close()
with open(output, mode='w', encoding='utf-8') as f:
f.write(txt)