From c11c0c0c6e40896627def046ca85add87cfcfd5e Mon Sep 17 00:00:00 2001 From: liuyebo <1515783401@qq.com> Date: Wed, 10 Jul 2024 14:58:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=96=87=E5=AD=97=E5=88=86?= =?UTF-8?q?=E5=89=B2=E6=97=B6=E7=9A=84=E6=96=B9=E5=90=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- photo_mask.py | 68 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/photo_mask.py b/photo_mask.py index 0cddb1e..b041672 100644 --- a/photo_mask.py +++ b/photo_mask.py @@ -184,32 +184,72 @@ def get_ocr_layout(ocr, img_path): def find_box_of_content(content, layout): full_box = layout[0] - box_len = full_box[2] - full_box[0] + x_len = full_box[2] - full_box[0] + y_len = full_box[3] - full_box[1] + if x_len >= y_len: + # 横向排布 + box_len = x_len + direction = "x" + else: + # 纵向排布 + box_len = y_len + direction = "y" text = layout[1] text_len = len(text) char_len = box_len / text_len index = text.index(content) - return ( - full_box[0] + index * char_len, - full_box[1], - full_box[0] + (index + len(content) + 1) * char_len, - full_box[3], - ) + + if direction == "x": + # 横向排布 + return ( + full_box[0] + index * char_len, + full_box[1], + full_box[0] + (index + len(content) + 1) * char_len, + full_box[3], + ) + else: + # 纵向排布 + return ( + full_box[0], + full_box[1] + index * char_len, + full_box[2], + full_box[1] + (index + len(content) + 1) * char_len, + ) def find_box_of_value(key, layout): full_box = layout[0] - box_len = full_box[2] - full_box[0] + x_len = full_box[2] - full_box[0] + y_len = full_box[3] - full_box[1] + if x_len >= y_len: + # 横向排布 + box_len = x_len + direction = "x" + else: + # 纵向排布 + box_len = y_len + direction = "y" text = layout[1] text_len = len(text) char_len = box_len / text_len index = text.index(key) - return ( - full_box[0] + (index + len(key)) * char_len, - full_box[1], - full_box[0] + (index + len(key) + 4) * char_len, - full_box[3], - ) + + if direction == "x": + # 横向排布 + return ( + full_box[0] + (index + len(key)) * char_len, + full_box[1], + full_box[0] + (index + len(key) + 4) * char_len, + full_box[3], + ) + else: + # 纵向排布 + return ( + full_box[0], + full_box[1] + (index + len(key)) * char_len, + full_box[2], + full_box[1] + (index + len(key) + 4) * char_len, + ) def get_mask_layout(image, content):