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

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

 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`错误。建议优先采用代码修改方案以兼容新版库。

相关文章:

  • MySQL COUNT(*) 查询优化详解!
  • AI文本分类
  • 【Web】LACTF 2025 wp
  • STM32CUBEIDE开发实战:ADC与UART应用
  • 【从零实现JsonRpc框架#3】线程模型与性能优化
  • Python----神经网络(《Deep Residual Learning for Image Recognition》论文和ResNet网络结构)
  • AI 驱动数据库交互技术路线详解:角色、提示词工程与输入输出分析
  • 计网学习笔记———网络
  • 圆角边框 盒子阴影 文字阴影
  • 线程互斥与线程同步
  • golang-ErrGroup用法以及源码解读笔记
  • Flutter - UIKit开发相关指南 - 概览
  • 手写系列——transformer网络完成加法和字符转译任务
  • Doris和Clickhouse对比
  • 智能时代下,水利安全员证如何引领行业变革?
  • day011-权限管理专题
  • ClassLoader类加载机制的核心引擎
  • 高效全能PDF工具,支持OCR识别
  • 前端HTMX技术详细解释
  • 如何创建伪服务器,伪接口
  • 巴防空系统击落印度无人机,印称巴方违反停火协议
  • “行人相撞案”现场视频公布,法院:表述不当造成误导
  • 中美经贸高层会谈将在午餐后继续
  • 白宫启动“返乡计划” ,鼓励非法移民自愿离开美国
  • 央行设立服务消费与养老再贷款,额度5000亿元
  • 习近平出席俄罗斯纪念苏联伟大卫国战争胜利80周年庆典