From 4805a4e4ae914a11030061a4a9752bd9505490a7 Mon Sep 17 00:00:00 2001 From: SherkeyXD <253294679@qq.com> Date: Tue, 10 Oct 2023 17:05:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E9=80=82=E9=85=8D=E6=96=B0=E4=B8=BB=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E4=B8=BB=E9=A2=98=E7=9A=84=E5=B0=8F=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/GetImageFromROI/dst/.gitkeep | 0 tools/GetImageFromROI/main.py | 80 ++++++++++++++++++++++++++ tools/GetImageFromROI/requirements.txt | 1 + tools/GetImageFromROI/src/.gitkeep | 0 tools/GetImageFromROI/start.bat | 2 + 5 files changed, 83 insertions(+) create mode 100644 tools/GetImageFromROI/dst/.gitkeep create mode 100644 tools/GetImageFromROI/main.py create mode 100644 tools/GetImageFromROI/requirements.txt create mode 100644 tools/GetImageFromROI/src/.gitkeep create mode 100644 tools/GetImageFromROI/start.bat diff --git a/tools/GetImageFromROI/dst/.gitkeep b/tools/GetImageFromROI/dst/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tools/GetImageFromROI/main.py b/tools/GetImageFromROI/main.py new file mode 100644 index 0000000000..3f710b99fd --- /dev/null +++ b/tools/GetImageFromROI/main.py @@ -0,0 +1,80 @@ +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 +std_ratio = std_width / std_height + +task_paths = { + "CN": Path("../../resource/tasks.json"), + "twxy": Path("../../resource/global/txwy/resource/tasks.json"), + "YoStarEN": Path("../../resource/global/YoStarEN/resource/tasks.json"), + "YoStarJP": Path("../../resource/global/YoStarJP/resource/tasks.json"), + "YoStarKR": Path("../../resource/global/YoStarKR/resource/tasks.json"), +} + +# Change this to your client +task_file = task_paths["CN"] + +# Add the tasks you want to get its image +task_list = [] +# You should get these names from tasks.json +# +# For example, when adding a new homepage theme: +task_list = [ + "Award", + "GachaEnter", + "Home", + "Infrast", + "OperBoxEnter", + "Recruit", + "Visit" +] + +with task_file.open("r", encoding="utf-8") as f: + tasks = json.load(f) + +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: # 否则可能是偏正方形的屏幕,按宽度计算 + dsize_width: int = std_width + dsize_height: int = std_width / cur_ratio + dsize = (dsize_width, dsize_height) + image = cv2.resize(image, dsize, interpolation=cv2.INTER_AREA) + + for i in task_list: + if type(tasks[i]["template"]) == str: + filename = tasks[i]["template"] + 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]] + + print("Saving", dst_path / filename) + cv2.imwrite(str(dst_path / filename), cropped) + + print("Finished processing file:", str(raw_image)) + diff --git a/tools/GetImageFromROI/requirements.txt b/tools/GetImageFromROI/requirements.txt new file mode 100644 index 0000000000..07d722477c --- /dev/null +++ b/tools/GetImageFromROI/requirements.txt @@ -0,0 +1 @@ +opencv-python~=4.5.3 diff --git a/tools/GetImageFromROI/src/.gitkeep b/tools/GetImageFromROI/src/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tools/GetImageFromROI/start.bat b/tools/GetImageFromROI/start.bat new file mode 100644 index 0000000000..134732e886 --- /dev/null +++ b/tools/GetImageFromROI/start.bat @@ -0,0 +1,2 @@ +python main.py +pause \ No newline at end of file