当前位置: 首页 > ai >正文

python:ASCII-generator 实用教程

开源项目网址:ASCII-generator-master
csdn下载网址:ASCII-generator-main.zip

参阅:手把手教你使用ASCII-Generator
where python
D:\Python37\python.exe

依赖库:
cv2
Pillow
numpy

pip show Pillow
Name: Pillow
Version: 9.3.0
Summary: Python Imaging Library (Fork)

zip包中有五个 python 脚本:
img2txt.py   图片生成文本
img2img.py 图片生成图片(黑白)
img2img_color.py 图片生成图片(彩色)
video2video.py 视频生成视频(黑白)
video2video_color.py 视频生成视频(彩色)

cd D:\test\ASCII-generator-master

D:\test\ASCII-generator-master> python img2txt.py -h
usage: Image to ASCII [-h] [--input INPUT] [--output OUTPUT][--mode {simple,complex}] [--num_cols NUM_COLS]optional arguments:-h, --help            show this help message and exit--input INPUT         Path to input image--output OUTPUT       Path to output text file--mode {simple,complex}10 or 70 different characters--num_cols NUM_COLS   number of character for output's width

--input   指输入文件。
--output 指输出文件。
--language 指生成ASCII的语言。
--background 指背景颜色,可设置为黑色或白色。
--num_cols 输出文件宽度

 运行 python img2txt.py --input input.jpg --output test1.txt --mode simple

下图为 jp2a 图像转换效果:

 

python img2img.py -h

python img2img.py --input input.jpg --output test1.jpg 
img2img.py:44: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.char_width, char_height = font.getsize(sample_character)

python img2img_color.py -h

D:\test\ASCII-generator-master> python img2img_color.py -h
usage: Image to ASCII [-h] [--input INPUT] [--output OUTPUT][--language LANGUAGE] [--mode MODE][--background {black,white}] [--num_cols NUM_COLS][--scale SCALE]optional arguments:-h, --help            show this help message and exit--input INPUT         Path to input image--output OUTPUT       Path to output text file--language LANGUAGE--mode MODE--background {black,white}background's color--num_cols NUM_COLS   number of character for output's width--scale SCALE         upsize output

运行  python img2img_color.py --input input.jpg --output test2.jpg 

python img2img_color.py --input 182_600x400.jpg --output color_182.jpg 

 python video2video.py -h 

D:\test\ASCII-generator-master> python video2video.py -h
usage: Image to ASCII [-h] [--input INPUT] [--output OUTPUT][--mode {simple,complex}] [--background {black,white}][--num_cols NUM_COLS] [--scale SCALE] [--fps FPS][--overlay_ratio OVERLAY_RATIO]optional arguments:-h, --help            show this help message and exit--input INPUT         Path to input video--output OUTPUT       Path to output video--mode {simple,complex}10 or 70 different characters--background {black,white}background's color--num_cols NUM_COLS   number of character for output's width--scale SCALE         upsize output--fps FPS             frame per second--overlay_ratio OVERLAY_RATIOOverlay width ratio

python video2video.py --input input2.mp4 --output test2.gif --mode simple  

python video2video_color.py -h 

D:\test\ASCII-generator-master> python video2video_color.py -h
usage: Image to ASCII [-h] [--input INPUT] [--output OUTPUT][--mode {simple,complex}] [--background {black,white}][--num_cols NUM_COLS] [--scale SCALE] [--fps FPS][--overlay_ratio OVERLAY_RATIO]optional arguments:-h, --help            show this help message and exit--input INPUT         Path to input video--output OUTPUT       Path to output video--mode {simple,complex}10 or 70 different characters--background {black,white}background's color--num_cols NUM_COLS   number of character for output's width--scale SCALE         upsize output--fps FPS             frame per second--overlay_ratio OVERLAY_RATIOOverlay width ratio


python img2img.py --input input.jpg --output test1.jpg
Traceback (most recent call last):
  File "D:\test\ASCII-generator-master\img2img.py", line 67, in <module>
    main(opt)
  File "D:\test\ASCII-generator-master\img2img.py", line 29, in main
    char_list, font, sample_character, scale = get_data(opt.language, opt.mode)
  File "D:\test\ASCII-generator-master\utils.py", line 117, in get_data
    char_list = sort_chars(char_list, font, language)
  File "D:\test\ASCII-generator-master\utils.py", line 13, in sort_chars
    char_width, char_height = font.getsize("A")
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
这个问题怎么处理?

出现该错误是因为新版Pillow库(≥10.0.0)移除了`getsize()`方法。你需要将代码中所有`font.getsize()`替换为`getbbox()`计算尺寸。以下是具体修复步骤:

---

**1. 修改img2img.py中的代码:**
找到这行代码:
```python
        char_width, char_height = font.getsize(sample_character)
```
替换为:
```python
bbox = font.getbbox(sample_character)
char_width = bbox[2] - bbox[0]
char_height = bbox[3] - bbox[1]
```

---

**2. 修改utils.py中的代码:**
找到这行代码(约在utils.py第13行):
```python
        char_width, char_height = font.getsize("A")
```
替换为:
```python
bbox = font.getbbox("A")
char_width = bbox[2] - bbox[0]
char_height = bbox[3] - bbox[1]
```

---

**3. 检查其他潜在调用:**
确保代码中没有其他`font.getsize()`的调用,若有则按相同方式替换。

---

**4. (可选)降级Pillow版本(临时方案):**
若不想修改代码,可执行:
```bash
pip install Pillow==9.3.0
```

---

完成上述修改后,重新运行程序即可解决`AttributeError`错误。建议优先采用代码修改方案以兼容新版库。

http://www.xdnf.cn/news/5271.html

相关文章:

  • $\int_{0}^{1} x \arcsin \sqrt{4x - 4x^2}dx$
  • LintCode第366题-斐波那契数列
  • 各种环境测试
  • 解释器和基于规则的系统比较
  • 【Linux基础】文件和目录管理指令
  • 对日开发 TeraTerm ttl脚本开发环境配置
  • python04——条件判断(选择结构)
  • 部署RocketMQ
  • 数孪实战笔记(1)数字孪生的含义、应用及技术体系
  • java-代理
  • [特殊字符] AI网关:大模型时代的智能交通指挥官 [特殊字符]
  • 科大讯飞TTS(文字转语音)和STT(语音转文字)
  • 如何将 Windows 11 的开始菜单移到左侧
  • ECMAScript 2017(ES2017):异步编程与对象操作的革新
  • CUDA编程——性能优化基本技巧
  • 常用的Linux命令100条
  • python 版本管理用的是pyenv pip install 把东西安装到那里了,好的检测方法,注意是windows环境
  • RENAME 语句与RENAME选项学习
  • 理解Yocto项目中`${D}`作为模拟目标系统根文件结构的临时目录
  • 投影显示技术全解析:主流方案对比与雷克赛恩 CyberPro1 的核心优势
  • 【桌面】【输入法】常见问题汇总
  • Day 14
  • 介绍一下synchronized锁升级过程
  • 2024年AI发展趋势全面解析:从多模态到AGI的突破
  • LintCode第485题-生成给定大小的数组,第220题-冰雹猜想,第235题-分解质因数
  • JDBC演进之路:从基础操作到高效连接池
  • 计算机科技笔记: 容错计算机设计03 系统可信性的度量 偶发故障期 浴盆曲线 韦布尔分布
  • 工程师视角下的 AI 浏览器智能体拆解(AI Browser Agent from an Engineer‘s Perspective)
  • TWAS、GWAS、FUSION
  • 使用Simulink开发Autosar Nvm存储逻辑