代码拉取完成,页面将自动刷新
同步操作将从 吴烜/图像字符化 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"""
@author: Viet Nguyen <nhviet1009@gmail.com>
"""
import argparse
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageOps
from utils import get_data
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.jpg", 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 args
def 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_cols
image = cv2.imread(opt.input, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width, _ = image.shape
cell_width = width / opt.num_cols
cell_height = scale * 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)
char_width, char_height = font.getsize(sample_character)
out_width = char_width * num_cols
out_height = scale * char_height * num_rows
out_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__':
opt = get_args()
main(opt)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。