独书先生 Menu

网页版 Python 图片转字符画

需求

最近发现一个好玩的 Python 玩法,用 Python 将图片转为字符画,大多数都是本地程序,因为之前小编搭建过一个在线版的 Python,于是尝试了使用在线版的 Python 实现了图片转字符画。

在线 Python 编辑器: https://lwebapp.com/zh/python-playground

思路

图片转字符画的基本思路是

  1. 使用 PIL 获得图片每个像素点的 RGB 颜色值
  2. 再将 RGB 颜色值映射为字符串
  3. 最后将字符串拼接起来就行,打印到终端或者保存为文本后就可以看到字符画了。

我们这里便于演示,统一用print方法打印字符。

实现

由于我们想要在网页上演示,就不能从本地加载图片了,我们选择直接从 base64 编码加载图片,或者从远程 URL 加载。我们这里再介绍下如何加载图片。

  1. 使用 base64 图片

用正则表达式将图片 base64 编码字符串头部信息去除,然后使用 base64 方法解码,BytesIO 配合 PIL 打开为一个 PIL 图片实例。

这里推荐一个小编的 在线图片转 base64 工具,支持直接粘贴图片就能转化为 base64 字符串,非常方便

# 图片 base64 字符串
img_base64 = ""
image_data = re.sub('^data:image/.+;base64,', '', img_base64)
# 打开图片
img = Image.open(BytesIO(base64.b64decode(image_data)))

接着对图片进行一点缩放防止图片太大,最后就是像素点转 RGB 再转字符画了,详细代码如下

import base64
import re
from io import BytesIO
from PIL import Image

# 随机字符串
char = list('M3NB6Q#OC?7>!:–;. ')

# 颜色值映射字符串
def get_char(r, g, b, alpha=256):
    if alpha == 0:
        return ' '

    grey = (2126 * r + 7152 * g + 722 * b) / 10000

    char_idx = int((grey / (alpha + 1.0)) * len(char))
    return char[char_idx]

# 图片 base64 字符串
img_base64 = '''data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAHR5JREFUeF7tnQvUPlVVxh9FES1CU9SCLmhKoHkBwbxLamgm2sULmFqCrZVkllqaWmpGeb+irWVgmgmplYK3zPsNTQJTC8VL1ApKRVOkC6FY6wdn/r7f+837zpmZvc85M3P2WrPeD/4z5+yzzzxzbns/+2qqUi1QLbDRAl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/Bh0RgLjUvwnLraECZLl9X1seYYEKkAgj1VuWa4EKkOX2fW15hAX+H2d/zQWBav7aAAAAAElFTkSuQmCC'''

# 正则删除头部信息,即 Data URI scheme
image_data = re.sub('^data:image/.+;base64,', '', img_base64)
# 打开图片
img = Image.open(BytesIO(base64.b64decode(image_data)))

img_widht = img.size[0]
img_height = img.size[1]

# 设定缩放比例
scale_width = 0.3  # 0.75
scale_height = 0.1  # 0.5

# 缩放图片
img = img.resize((int(img_widht*scale_width),
                 int(img_height*scale_height)), Image.NEAREST)

# 输出的字符画
text = ''
for i in range(int(img_height*scale_height)):
    for j in range(int(img_widht*scale_width)):
        text += get_char(*img.getpixel((j, i)))
    text += '\n'

print(text)

在线 Demo:Python 图片转字符画-base64 图片

  1. 从 URL 加载图片

如果是本地应用,我们可以用 requests 这个 Python 内置库来加载网络图片,由于这个在线 Python 工具使用的是 pyodide,并不支持 requests ,所以需要使用官方提供的 API pyodide.http.pyfetch 来实现网络请求

response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')

拿到图片数据之后也是用 PIL 打开即可,详细代码如下

from io import BytesIO
from PIL import Image
import pyodide

char = list('M3NB6Q#OC?7>!:–;. ')


def get_char(r, g, b, alpha=256):
    if alpha == 0:
        return ' '

    grey = (2126 * r + 7152 * g + 722 * b) / 10000

    char_idx = int((grey / (alpha + 1.0)) * len(char))
    return char[char_idx]


def write_file(out_file_name, content):
    with open(out_file_name, 'w') as f:
        f.write(content)

# pyodide 提供的Python API,用于网络请求,reference: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch
response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')

image_data = await response.bytes()
img = Image.open(BytesIO(image_data))

img_widht = img.size[0]
img_height = img.size[1]

scale_width = 0.3  # 0.75
scale_height = 0.1  # 0.5

img = img.resize((int(img_widht*scale_width),
                 int(img_height*scale_height)), Image.NEAREST)

text = ''
for i in range(int(img_height*scale_height)):
    for j in range(int(img_widht*scale_width)):
        text += get_char(*img.getpixel((j, i)))
    text += '\n'

print(text)

在线 Demo:Python 图片转字符画-URL 图片

总结

以上就是小编总结的 Python 图片转字符画的方法了,并且可以运行在网页上,把链接分享给你的朋友。其中可能还有些不足,欢迎探讨。后续还有更多好玩的 Python 用法,欢迎关注小编的更新。

参考