Python用图片生成banner.txt文件

Python用图片生成banner.txt文件

1. 代码

python 复制代码
#!/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. 感受

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

相关推荐
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 38--Allure 测试报告
python·学习·测试工具·pytest
沈浩(种子思维作者)8 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
njsgcs9 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
io_T_T9 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
华研前沿标杆游学9 小时前
2026年走进洛阳格力工厂参观游学
python
Carl_奕然9 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析
AI小怪兽10 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机
wszy180910 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
Eric.Lee202110 小时前
python实现 mp4转gif文件
开发语言·python·手势识别·手势交互·手势建模·xr混合现实
EntyIU10 小时前
python开发中虚拟环境配置
开发语言·python