Python用图片生成banner.txt文件

Python用图片生成banner.txt文件

1. 代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/7/1 17:18
# @Author  : ning
# @File    : generateBanner.py
# @Software: PyCharm
from PIL import Image


def process():
    print(f'Hello, World!')
    # 打开图片
    image = Image.open(r'logo.png').convert('L')  # 将图片转为灰度模式,这里需把'your_image_path.jpg'替换为你实际图片路径

    # 设置缩放比例,例如0.2表示将图片缩小为原来的20%,可按需调整
    scale = 0.2
    new_width = int(image.width * 0.1)
    new_height = int(image.height * 0.05)
    image = image.resize((new_width, new_height))

    width, height = image.size

    # 定义用于替换像素的字符集,从亮到暗
    ascii_chars = [' ', '.', '-', '=', '+', '*', '#', '%', '@']

    result = []
    for y in range(height):
        line = ""
        for x in range(width):
            pixel = image.getpixel((x, y))
            # 根据像素值映射到字符集
            char_index = int(pixel / 256 * len(ascii_chars))
            line += ascii_chars[char_index]
        result.append(line)

    # 将结果写入文件
    with open('banner.txt', 'w') as f:
        for line in result:
            f.write(line + '\n')


if __name__ == '__main__':
    process()

2. 感受

还可以吧,对比度强的使用体验还不错,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值