feat: GetImageFromRoi小工具支持直接上mask

This commit is contained in:
SherkeyXD
2023-11-06 12:57:48 +08:00
parent 5d6de2bc56
commit 5040e0aeb3
2 changed files with 92 additions and 59 deletions

View File

@@ -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",

View File

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