图片转字符画(python)

记录 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值