From 5040e0aeb3bcb07ed868c4e48765e11f75ef1d30 Mon Sep 17 00:00:00 2001 From: SherkeyXD <253294679@qq.com> Date: Mon, 6 Nov 2023 12:57:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20GetImageFromRoi=E5=B0=8F=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=94=AF=E6=8C=81=E7=9B=B4=E6=8E=A5=E4=B8=8Amask?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/tasks.json | 120 +++++++++++++++++++++------------- tools/GetImageFromROI/main.py | 31 +++++---- 2 files changed, 92 insertions(+), 59 deletions(-) diff --git a/resource/tasks.json b/resource/tasks.json index 9de9a065af..2e8e6bd896 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -4639,12 +4639,20 @@ 200, 150 ], - "roi_crop_doc": [ - 935, - 480, - 145, - 61 - ], + "crop_doc": { + "roi": [ + 935, + 480, + 145, + 61 + ], + "mask": [ + 125, + 0, + 25, + 20 + ] + }, "maskRange": [ 1, 255 @@ -4989,19 +4997,22 @@ 130, 130 ], - "roi_crop_doc": [ - 720, - 553, - 75, - 70 - ], "next": [ "ReceiveAward", "DailyTask", "WeeklyTask", "Award", "Award@CloseAnnos#next" - ] + ], + "crop_doc": { + "roi": [ + 720, + 553, + 75, + 70 + ], + "mask": [] + } }, "ReceiveAward": { "action": "ClickSelf", @@ -5192,17 +5203,20 @@ 200, 150 ], - "roi_crop_doc": [ - 300, - 550, - 128, - 41 - ], "next": [ "FriendsList", "Visit", "Visit@CloseAnnos#next" - ] + ], + "crop_doc": { + "roi": [ + 300, + 550, + 128, + 41 + ], + "mask": [] + } }, "FriendsList": { "action": "ClickSelf", @@ -5430,12 +5444,15 @@ 180, 120 ], - "roi_crop_doc": [ - 903, - 120, - 97, - 58 - ] + "crop_doc": { + "roi": [ + 903, + 120, + 97, + 58 + ], + "mask": [] + } }, "CreditShop-Commodities": { "template": "CreditPoint.png", @@ -5700,12 +5717,6 @@ 160, 160 ], - "roi_crop_doc": [ - 935, - 570, - 85, - 75 - ], "action": "ClickSelf", "next": [ "InfrastEnteredFlag", @@ -5713,7 +5724,16 @@ "Infrast@CloseAnnos#next", "Infrast@OfflineConfirm" ], - "postDelay": 5000 + "postDelay": 5000, + "crop_doc": { + "roi": [ + 935, + 570, + 85, + 75 + ], + "mask": [] + } }, "InfrastEnteredFlag": { "template": "InfrastOverview.png", @@ -8880,16 +8900,19 @@ 265, 110 ], - "roi_crop_doc": [ - 945, - 301, - 85, - 78 - ], "next": [ "OperBoxRoleTabSelect", "OperBoxRoleTab" - ] + ], + "crop_doc": { + "roi": [ + 945, + 301, + 85, + 78 + ], + "mask": [] + } }, "OperBoxRoleTabSelect": { "action": "ClickSelf", @@ -9862,17 +9885,20 @@ 228, 156 ], - "roi_crop_doc": [ - 1095, - 497, - 167, - 56 - ], "next": [ "GachaEnter", "GachaEnter@LoadingText", "DoGacha" - ] + ], + "crop_doc": { + "roi": [ + 1095, + 497, + 167, + 56 + ], + "mask": [] + } }, "GachaLastOnceResult": { "algorithm": "OcrDetect", diff --git a/tools/GetImageFromROI/main.py b/tools/GetImageFromROI/main.py index d1e34fab50..58e58e4864 100644 --- a/tools/GetImageFromROI/main.py +++ b/tools/GetImageFromROI/main.py @@ -2,13 +2,13 @@ import cv2 import json from pathlib import Path -''' +""" This is a tool for cropping one image into several images based on current roi. Use this tool when a raw image contains many templates. For example, when adding a new homepage theme. Put one picture in ./src and add the template's tasks to 'task_list', This tools will try to read roi (if not 'roi_crop_doc') from tasks in tasks.json and then crop the image. -''' +""" std_width: int = 1280 std_height: int = 720 @@ -37,7 +37,7 @@ task_list = [ "Infrast", "OperBoxEnter", "Recruit", - "Visit" + "Visit", ] with task_file.open("r", encoding="utf-8") as f: @@ -47,15 +47,13 @@ src_path = Path("./src") dst_path = Path("./dst") for raw_image in src_path.glob("*.png"): - print("Processing file:", str(raw_image)) - image = cv2.imread(str(raw_image)) cur_ratio = image.shape[1] / image.shape[0] if cur_ratio >= std_ratio: # 说明是宽屏或默认16:9,按照高度计算缩放 dsize_width: int = (int)(cur_ratio * std_height) dsize_height: int = std_height - else: # 否则可能是偏正方形的屏幕,按宽度计算 + else: # 否则可能是偏正方形的屏幕,按宽度计算 dsize_width: int = std_width dsize_height: int = std_width / cur_ratio dsize = (dsize_width, dsize_height) @@ -69,14 +67,23 @@ for raw_image in src_path.glob("*.png"): elif type(tasks[i]["template"]) == list: # this is for multi-template: filename = tasks[i]["template"][0].split(".")[0] + raw_image.stem + ".png" - try: - roi = tasks[i]["roi_crop_doc"] - except KeyError: - roi = tasks[i]["roi"] - cropped = image[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2]] + + crop_doc = tasks[i].get("crop_doc", {}) + roi = crop_doc.get("roi", tasks[i]["roi"]) + mask = crop_doc.get("mask", []) + + cropped = image[roi[1] : roi[1] + roi[3], roi[0] : roi[0] + roi[2]] + + if len(mask) > 0: + cv2.rectangle( + cropped, + (mask[0], mask[1]), + (mask[0] + mask[2], mask[1] + mask[3]), + (0, 0, 0), + -1, + ) print("Saving", dst_path / filename) cv2.imwrite(str(dst_path / filename), cropped) print("Finished processing file:", str(raw_image)) -