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. 感受

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

相关推荐
计算机学长felix4 分钟前
基于Django的“酒店推荐系统”设计与开发(源码+数据库+文档+PPT)
数据库·python·mysql·django·vue
站大爷IP5 分钟前
Python随机数函数全解析:5个核心工具的实战指南
python
悟乙己13 分钟前
使用 Python 中的强化学习最大化简单 RAG 性能
开发语言·python·agent·rag·n8n
max50060018 分钟前
图像处理:实现多图点重叠效果
开发语言·图像处理·人工智能·python·深度学习·音视频
AI原吾29 分钟前
玩转物联网只需十行代码,可它为何悄悄停止维护
python·物联网·hbmqtt
云动雨颤36 分钟前
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
python·单元测试
SunnyDays10111 小时前
Python 实现 HTML 转 Word 和 PDF
python·html转word·html转pdf·html转docx·html转doc
跟橙姐学代码2 小时前
Python异常处理:告别程序崩溃,让代码更优雅!
前端·python·ipython
蓝纹绿茶2 小时前
Python程序使用了Ffmpeg,结束程序后,文件夹中仍然生成音频、视频文件
python·ubuntu·ffmpeg·音视频
mahuifa2 小时前
OpenCV 开发 -- 图像基本处理
人工智能·python·opencv·计算机视觉