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

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

相关推荐
Vertira8 分钟前
pdf 合并 python实现(已解决)
前端·python·pdf
太凉12 分钟前
Python之 sorted() 函数的基本语法
python
项目題供诗29 分钟前
黑马python(二十四)
开发语言·python
晓13131 小时前
OpenCV篇——项目(二)OCR文档扫描
人工智能·python·opencv·pycharm·ocr
是小王同学啊~1 小时前
(LangChain)RAG系统链路向量检索器之Retrievers(五)
python·算法·langchain
AIGC包拥它1 小时前
提示技术系列——链式提示
人工智能·python·langchain·prompt
孟陬1 小时前
Python matplotlib 如何**同时**展示正文和 emoji
python
何双新1 小时前
第 1 课:Flask 简介与环境配置(Markdown 教案)
后端·python·flask
费弗里2 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(2)
python·dash
吴佳浩2 小时前
Python入门指南-AI番外-MCP完整教程:从零开始学会Model Context Protocol
人工智能·python·mcp