1 Star 0 Fork 1

云上的开阔者/ASCII-generator

forked from 李乙平/ASCII-generator 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
img2txt.py 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
vietnguyen91 提交于 2019-01-31 19:15 . upload
"""
@author: Viet Nguyen <nhviet1009@gmail.com>
"""
import argparse
import cv2
import numpy as np
def get_args():
parser = argparse.ArgumentParser("Image to ASCII")
parser.add_argument("--input", type=str, default="data/input.jpg", help="Path to input image")
parser.add_argument("--output", type=str, default="data/output.txt", help="Path to output text file")
parser.add_argument("--mode", type=str, default="complex", choices=["simple", "complex"],
help="10 or 70 different characters")
parser.add_argument("--num_cols", type=int, default=150, help="number of character for output's width")
args = parser.parse_args()
return args
def main(opt):
if opt.mode == "simple":
CHAR_LIST = '@%#*+=-:. '
else:
CHAR_LIST = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
num_chars = len(CHAR_LIST)
num_cols = opt.num_cols
image = cv2.imread(opt.input)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
height, width = image.shape
cell_width = width / opt.num_cols
cell_height = 2 * cell_width
num_rows = int(height / cell_height)
if num_cols > width or num_rows > height:
print("Too many columns or rows. Use default setting")
cell_width = 6
cell_height = 12
num_cols = int(width / cell_width)
num_rows = int(height / cell_height)
output_file = open(opt.output, 'w')
for i in range(num_rows):
for j in range(num_cols):
output_file.write(
CHAR_LIST[min(int(np.mean(image[int(i * cell_height):min(int((i + 1) * cell_height), height),
int(j * cell_width):min(int((j + 1) * cell_width),
width)]) * num_chars / 255), num_chars - 1)])
output_file.write("\n")
output_file.close()
if __name__ == '__main__':
opt = get_args()
main(opt)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/XlSmallMing/ASCII-generator.git
git@gitee.com:XlSmallMing/ASCII-generator.git
XlSmallMing
ASCII-generator
ASCII-generator
master

搜索帮助