目录
- 预览效果
- 安装环境
- 报错分析
- 基本例程
- 总结
欢迎关注 『Python』 系列,持续更新中
欢迎关注 『Python』 系列,持续更新中
预览效果
-
原图
-
黑白图
-
彩色图
安装环境
- 拉取代码
https://github.com/vietnh1009/ASCII-generator
- python3.8
pip install opencv-python numpy Pillow
报错分析
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
getsize这个方法是老版本的 pillow解决方法有2中
- 法1:降级 Pillow(推荐)
pip install Pillow==9.5.0
降级后运行有警告,不用理会正常运行
- 法2(不推荐,可能还要改哪里):把所有代码中的
getsize
替换成.getbbox
我试了一下 发现不知道为啥效果变差了,宽度不对应了,效果变差了
基本例程
img2img_color.py
python">"""
@author: Viet Nguyen <nhviet1009@gmail.com>
"""
import argparseimport cv2
import numpy as np
from PIL import Image, ImageDraw, ImageOps
from utils import get_datadef get_args(inputImg='input.jpg',outImg='output.jpg'):parser = argparse.ArgumentParser("Image to ASCII")parser.add_argument("--input", type=str, default="data/"+inputImg, help="Path to input image")parser.add_argument("--output", type=str, default="data/"+outImg, help="Path to output text file")parser.add_argument("--language", type=str, default="english")parser.add_argument("--mode", type=str, default="standard")parser.add_argument("--background", type=str, default="black", choices=["black", "white"],help="background's color")parser.add_argument("--num_cols", type=int, default=300, help="number of character for output's width")parser.add_argument("--scale", type=int, default=2, help="upsize output")args = parser.parse_args()return argsdef main(opt):if opt.background == "white":bg_code = (255, 255, 255)else:bg_code = (0, 0, 0)char_list, font, sample_character, scale = get_data(opt.language, opt.mode)num_chars = len(char_list)num_cols = opt.num_colsimage = cv2.imread(opt.input, cv2.IMREAD_COLOR)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)height, width, _ = image.shapecell_width = width / opt.num_colscell_height = scale * cell_widthnum_rows = int(height / cell_height)if num_cols > width or num_rows > height:print("Too many columns or rows. Use default setting")cell_width = 6cell_height = 12num_cols = int(width / cell_width)num_rows = int(height / cell_height)char_width, char_height = font.getsize(sample_character)out_width = char_width * num_colsout_height = scale * char_height * num_rowsout_image = Image.new("RGB", (out_width, out_height), bg_code)draw = ImageDraw.Draw(out_image)for i in range(num_rows):for j in range(num_cols):partial_image = image[int(i * cell_height):min(int((i + 1) * cell_height), height),int(j * cell_width):min(int((j + 1) * cell_width), width), :]partial_avg_color = np.sum(np.sum(partial_image, axis=0), axis=0) / (cell_height * cell_width)partial_avg_color = tuple(partial_avg_color.astype(np.int32).tolist())char = char_list[min(int(np.mean(partial_image) * num_chars / 255), num_chars - 1)]draw.text((j * char_width, i * char_height), char, fill=partial_avg_color, font=font)if opt.background == "white":cropped_image = ImageOps.invert(out_image).getbbox()else:cropped_image = out_image.getbbox()out_image = out_image.crop(cropped_image)out_image.save(opt.output)if __name__ == '__main__':inputImg='csdn.png'outImg='csdn-output.png'opt = get_args(inputImg,outImg)main(opt)
总结
大家喜欢的话,给个👍,点个关注!继续跟大家分享敲代码过程中遇到的问题!
版权声明:
发现你走远了@mzh原创作品,转载必须标注原文链接
Copyright 2024 mzh
Crated:2024-11-04
欢迎关注 『Python』 系列,持续更新中
欢迎关注 『Python』 系列,持续更新中
【Python安装第三方库一行命令永久提高速度】
【使用PyInstaller打包Python文件】
【更多内容敬请期待】