diff --git a/img2img.py b/img2img.py index 06936104a360bb9eb777e12cbc1ae14c4b8a5e87..910a4d3c3020ed783da877a4083a4cf8f01e4e89 100644 --- a/img2img.py +++ b/img2img.py @@ -26,24 +26,27 @@ def main(opt): bg_code = 255 else: bg_code = 0 - char_list, font, sample_character, scale = get_data(opt.language, opt.mode) + char_list, font, 字符示例, scale = get_data(opt.language, opt.mode) 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 + + 字符宽度, 字符高度 = font.getsize(字符示例) + 字符高宽比 = 字符高度/字符宽度 + cell_width = width / opt.num_cols - cell_height = scale * cell_width + cell_height = 字符高宽比 * 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 + cell_height = 字符高宽比 * cell_width 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_width = 字符宽度 * num_cols + out_height = scale * 字符高度 * num_rows out_image = Image.new("L", (out_width, out_height), bg_code) draw = ImageDraw.Draw(out_image) for i in range(num_rows): @@ -52,7 +55,7 @@ def main(opt): width)]) / 255 * num_chars), num_chars - 1)] for j in range(num_cols)]) + "\n" - draw.text((0, i * char_height), line, fill=255 - bg_code, font=font) + draw.text((0, i * 字符高度), line, fill=255 - bg_code, font=font) if opt.background == "white": cropped_image = ImageOps.invert(out_image).getbbox() diff --git a/img2img_color.py b/img2img_color.py index 86a0768ea3a51eb1dff61fde345e5ee6940cf7d3..a0818a2d2ff0fd91b13988f2dd05d2ec0456703c 100644 --- a/img2img_color.py +++ b/img2img_color.py @@ -28,24 +28,27 @@ def main(opt): bg_code = (255, 255, 255) else: bg_code = (0, 0, 0) - char_list, font, sample_character, scale = get_data(opt.language, opt.mode) + char_list, font, 字符示例, 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 + + 字符宽度, 字符高度 = font.getsize(字符示例) + 字符高宽比 = 字符高度/字符宽度 + cell_width = width / opt.num_cols - cell_height = scale * cell_width + cell_height = 字符高宽比 * 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 + cell_height = 字符高宽比 * cell_width 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_width = 字符宽度 * num_cols + out_height = scale * 字符高度 * num_rows out_image = Image.new("RGB", (out_width, out_height), bg_code) draw = ImageDraw.Draw(out_image) for i in range(num_rows): @@ -55,7 +58,7 @@ def main(opt): 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) + draw.text((j * 字符宽度, i * 字符高度), char, fill=partial_avg_color, font=font) if opt.background == "white": cropped_image = ImageOps.invert(out_image).getbbox()