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):