From 8098344ce97f22ea368d37edd44f0102bcfcd60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Sun, 24 Nov 2024 09:03:44 -0800 Subject: [PATCH 1/2] =?UTF-8?q?#IB694K=20=E9=BB=91=E7=99=BD=E7=85=A7?= =?UTF-8?q?=E9=AB=98=E5=AE=BD=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- img2img.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/img2img.py b/img2img.py index 0693610..910a4d3 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() -- Gitee From a5f51a73e10b7c695f34fe491e9bfac3eb12389f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=83=9C?= Date: Sun, 24 Nov 2024 09:20:58 -0800 Subject: [PATCH 2/2] =?UTF-8?q?#IB694K=20=E5=BD=A9=E8=89=B2=E9=AB=98?= =?UTF-8?q?=E5=AE=BD=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- img2img_color.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/img2img_color.py b/img2img_color.py index 86a0768..a0818a2 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() -- Gitee