crop-icon 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Your Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,87 @@
1
+ Metadata-Version: 2.4
2
+ Name: crop_icon
3
+ Version: 0.2.1
4
+ Summary: Interactive (mouse drag) command-line tool to crop icon templates from a screenshot
5
+ Author-email: Your Name <you@example.com>
6
+ License: MIT
7
+ Keywords: icon,crop,opencv,template-matching,screenshot
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Multimedia :: Graphics
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: opencv-python>=4.5
16
+ Requires-Dist: numpy
17
+ Provides-Extra: dev
18
+ Requires-Dist: build; extra == "dev"
19
+ Requires-Dist: twine; extra == "dev"
20
+ Dynamic: license-file
21
+
22
+ # crop-icon
23
+
24
+ 命令行 **icon 模板截取工具(手动拖框)**:弹窗显示参考截图,用鼠标拖框选择区域,
25
+ 按 `s` 后在终端输入文件名,即可把该区域保存为一张 icon 模板图片,供模板匹配 / 图标识别流程使用。
26
+ 仅负责截模板,不导出任何坐标 JSON。
27
+
28
+ ## 安装
29
+
30
+ ```bash
31
+ pip install crop-icon
32
+ ```
33
+
34
+ 安装时会自动下载依赖 `opencv-python`、`numpy`。
35
+
36
+ > 需要图形界面:Linux 上要有桌面或 X11 转发;若报缺少 `libGL`,
37
+ > 执行 `sudo apt install libgl1`。
38
+
39
+ ## 用法
40
+
41
+ ```bash
42
+ crop-icon --image reference.png --out-dir ./icon
43
+ ```
44
+
45
+ 弹出图像窗口后:
46
+
47
+ 1. **鼠标拖框** 选择要截取的区域;
48
+ 2. 按 **`s`**,在终端输入模板文件名(如 `nav_arrow`,不带扩展名会自动补 `.png`)→ 保存到 `out-dir`;
49
+ 3. 重复 1–2 截取多个模板;
50
+ 4. 按 **`q`** 退出。
51
+
52
+ 也可以用 `python -m crop_icon ...` 运行。
53
+
54
+ ### 按键
55
+
56
+ | 按键 | 作用 |
57
+ |---|---|
58
+ | 鼠标拖框 | 选择区域 |
59
+ | `s` | 保存当前选区为 icon 模板(终端输入文件名) |
60
+ | `n` | 清除当前选区 |
61
+ | `q` | 退出 |
62
+
63
+ ### 参数
64
+
65
+ | 参数 | 默认 | 说明 |
66
+ |---|---|---|
67
+ | `--image` | 必需 | 参考截图路径 |
68
+ | `--out-dir` | `./icon` | 模板输出目录(相对当前工作目录) |
69
+ | `--no-resize` | 关闭 | 不缩放到 1280×720,直接按原图操作 |
70
+
71
+ ### 说明
72
+
73
+ - 默认先把图缩放到 **1280×720** 再裁剪;用 `--no-resize` 关闭。
74
+ - 文件名不带扩展名时自动补 `.png`;名称可含中文(如 `激活icon.png`)。
75
+ - 选区小于 5×5 会被拒绝;超出图像边界会裁剪到边界。
76
+
77
+ ## 开发 / 发布
78
+
79
+ ```bash
80
+ pip install -e .[dev] # 可编辑安装 + build/twine
81
+ python -m build # 构建 dist/*.whl 与 *.tar.gz
82
+ twine upload dist/* # 发布到 PyPI
83
+ ```
84
+
85
+ ## 许可
86
+
87
+ MIT
@@ -0,0 +1,66 @@
1
+ # crop-icon
2
+
3
+ 命令行 **icon 模板截取工具(手动拖框)**:弹窗显示参考截图,用鼠标拖框选择区域,
4
+ 按 `s` 后在终端输入文件名,即可把该区域保存为一张 icon 模板图片,供模板匹配 / 图标识别流程使用。
5
+ 仅负责截模板,不导出任何坐标 JSON。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ pip install crop-icon
11
+ ```
12
+
13
+ 安装时会自动下载依赖 `opencv-python`、`numpy`。
14
+
15
+ > 需要图形界面:Linux 上要有桌面或 X11 转发;若报缺少 `libGL`,
16
+ > 执行 `sudo apt install libgl1`。
17
+
18
+ ## 用法
19
+
20
+ ```bash
21
+ crop-icon --image reference.png --out-dir ./icon
22
+ ```
23
+
24
+ 弹出图像窗口后:
25
+
26
+ 1. **鼠标拖框** 选择要截取的区域;
27
+ 2. 按 **`s`**,在终端输入模板文件名(如 `nav_arrow`,不带扩展名会自动补 `.png`)→ 保存到 `out-dir`;
28
+ 3. 重复 1–2 截取多个模板;
29
+ 4. 按 **`q`** 退出。
30
+
31
+ 也可以用 `python -m crop_icon ...` 运行。
32
+
33
+ ### 按键
34
+
35
+ | 按键 | 作用 |
36
+ |---|---|
37
+ | 鼠标拖框 | 选择区域 |
38
+ | `s` | 保存当前选区为 icon 模板(终端输入文件名) |
39
+ | `n` | 清除当前选区 |
40
+ | `q` | 退出 |
41
+
42
+ ### 参数
43
+
44
+ | 参数 | 默认 | 说明 |
45
+ |---|---|---|
46
+ | `--image` | 必需 | 参考截图路径 |
47
+ | `--out-dir` | `./icon` | 模板输出目录(相对当前工作目录) |
48
+ | `--no-resize` | 关闭 | 不缩放到 1280×720,直接按原图操作 |
49
+
50
+ ### 说明
51
+
52
+ - 默认先把图缩放到 **1280×720** 再裁剪;用 `--no-resize` 关闭。
53
+ - 文件名不带扩展名时自动补 `.png`;名称可含中文(如 `激活icon.png`)。
54
+ - 选区小于 5×5 会被拒绝;超出图像边界会裁剪到边界。
55
+
56
+ ## 开发 / 发布
57
+
58
+ ```bash
59
+ pip install -e .[dev] # 可编辑安装 + build/twine
60
+ python -m build # 构建 dist/*.whl 与 *.tar.gz
61
+ twine upload dist/* # 发布到 PyPI
62
+ ```
63
+
64
+ ## 许可
65
+
66
+ MIT
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "crop_icon"
7
+ version = "0.2.1"
8
+ description = "Interactive (mouse drag) command-line tool to crop icon templates from a screenshot"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "Your Name", email = "you@example.com" }]
13
+ keywords = ["icon", "crop", "opencv", "template-matching", "screenshot"]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ "Topic :: Multimedia :: Graphics",
19
+ ]
20
+ dependencies = [
21
+ "opencv-python>=4.5",
22
+ "numpy",
23
+ ]
24
+
25
+ [project.scripts]
26
+ crop-icon = "crop_icon.cli:main"
27
+
28
+ [project.optional-dependencies]
29
+ dev = ["build", "twine"]
30
+
31
+ [tool.setuptools.packages.find]
32
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+ """crop_icon —— 命令行 icon 模板截取工具。"""
3
+
4
+ __version__ = "0.2.1"
@@ -0,0 +1,8 @@
1
+ # -*- coding: utf-8 -*-
2
+ """支持 `python -m crop_icon` 方式运行。"""
3
+ import sys
4
+
5
+ from .cli import main
6
+
7
+ if __name__ == "__main__":
8
+ sys.exit(main())
@@ -0,0 +1,213 @@
1
+ # -*- coding: utf-8 -*-
2
+ """命令行 icon 模板截取工具主逻辑(手动拖框交互)。
3
+
4
+ 弹窗显示参考截图,用鼠标拖框选择区域,按 s 后在终端输入文件名保存为 icon 模板。
5
+ 只负责截 icon 模板,不导出任何坐标 JSON。依赖(opencv-python、numpy)由包安装时声明安装。
6
+
7
+ 用法:
8
+ crop-icon --image reference.png --out-dir ./icon
9
+
10
+ 按键:
11
+ 鼠标拖框 → 选择区域
12
+ s → 保存当前选区为 icon 模板(在终端输入文件名)
13
+ n → 清除当前选区
14
+ q → 退出
15
+
16
+ 说明:
17
+ - 默认先把图缩放到 1280×720 再裁剪;--no-resize 关闭。
18
+ - 文件名不带扩展名时自动补 .png;可含中文。
19
+ - Linux 需图形界面(或 X11 转发);缺 libGL 时:sudo apt install libgl1
20
+ """
21
+ import argparse
22
+ import os
23
+ import sys
24
+
25
+ import cv2
26
+ import numpy as np
27
+
28
+
29
+ def resize_to_720p(frame):
30
+ """将帧缩放到 1280×720(保持宽高比填充黑边),返回 (resized_frame, scale)。"""
31
+ h, w = frame.shape[:2]
32
+ target_w, target_h = 1280, 720
33
+ if w == target_w and h == target_h:
34
+ return frame, 1.0
35
+ scale = min(target_w / w, target_h / h)
36
+ new_w = int(w * scale)
37
+ new_h = int(h * scale)
38
+ resized = cv2.resize(frame, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
39
+ canvas = np.zeros((target_h, target_w, 3), dtype=np.uint8)
40
+ x_off = (target_w - new_w) // 2
41
+ y_off = (target_h - new_h) // 2
42
+ canvas[y_off:y_off + new_h, x_off:x_off + new_w] = resized
43
+ return canvas, scale
44
+
45
+
46
+ def normalize_rect(rect):
47
+ """把 (x0,y0,x1,y1) 规范化成 (x, y, w, h)。"""
48
+ x0, y0, x1, y1 = rect
49
+ x, y = min(x0, x1), min(y0, y1)
50
+ w, h = abs(x1 - x0), abs(y1 - y0)
51
+ return x, y, w, h
52
+
53
+
54
+ def save_roi(img, rect, name, out_dir):
55
+ """把 rect=(x0,y0,x1,y1) 选区保存为模板图片,返回保存路径。
56
+
57
+ 独立于 GUI,便于直接调用测试。用 cv2.imencode + 二进制写以支持中文文件名。
58
+ """
59
+ x, y, w, h = normalize_rect(rect)
60
+ if w < 5 or h < 5:
61
+ raise ValueError("选择区域太小(至少 5×5 像素),当前 %d×%d" % (w, h))
62
+ name = (name or "").strip()
63
+ if not name:
64
+ raise ValueError("文件名不能为空")
65
+ if not name.lower().endswith((".png", ".jpg", ".jpeg", ".bmp")):
66
+ name += ".png"
67
+
68
+ ih, iw = img.shape[:2]
69
+ x0, y0 = max(0, x), max(0, y)
70
+ x1, y1 = min(iw, x + w), min(ih, y + h)
71
+ roi = img[y0:y1, x0:x1]
72
+
73
+ ext = os.path.splitext(name)[1].lower()
74
+ ok, buf = cv2.imencode(ext, roi)
75
+ if not ok:
76
+ raise RuntimeError("图像编码失败: %s" % name)
77
+
78
+ os.makedirs(out_dir, exist_ok=True)
79
+ save_path = os.path.join(out_dir, name)
80
+ with open(save_path, "wb") as f:
81
+ f.write(buf.tobytes())
82
+ return save_path
83
+
84
+
85
+ class _IconCropper:
86
+ """鼠标拖框 + 按键保存的交互式 icon 模板裁剪器。"""
87
+
88
+ def __init__(self, img, out_dir, win="crop-icon"):
89
+ self.img = img
90
+ self.out_dir = out_dir
91
+ self.win = win
92
+ self.drag_start = None
93
+ self.rect = None
94
+ self.drawing = False
95
+ self.saved = [] # [(文件名, rect), ...]
96
+ self._show_help()
97
+
98
+ def _show_help(self):
99
+ print("=" * 50)
100
+ print("手动截取 icon 模板")
101
+ print("=" * 50)
102
+ print("输出目录: %s" % self.out_dir)
103
+ print(" 鼠标拖框 → 选择区域")
104
+ print(" s → 保存当前选区为模板(终端输入文件名)")
105
+ print(" n → 清除当前选区")
106
+ print(" q → 退出")
107
+ print("=" * 50)
108
+
109
+ def on_mouse(self, event, x, y, flags, param):
110
+ if event == cv2.EVENT_LBUTTONDOWN:
111
+ self.drawing = True
112
+ self.drag_start = (x, y)
113
+ elif event == cv2.EVENT_MOUSEMOVE and self.drawing:
114
+ self.rect = (self.drag_start[0], self.drag_start[1], x, y)
115
+ elif event == cv2.EVENT_LBUTTONUP:
116
+ self.drawing = False
117
+ self.rect = (self.drag_start[0], self.drag_start[1], x, y)
118
+
119
+ def _save_current(self):
120
+ if not self.rect:
121
+ print("请先拖框选择区域!")
122
+ return
123
+ _, _, w, h = normalize_rect(self.rect)
124
+ if w < 5 or h < 5:
125
+ print("选择区域太小(至少 5×5),请重新选择")
126
+ return
127
+ try:
128
+ name = input("模板文件名 (如 nav_arrow,默认 .png): ").strip()
129
+ except EOFError:
130
+ print("无法读取输入,已取消保存")
131
+ return
132
+ try:
133
+ path = save_roi(self.img, self.rect, name, self.out_dir)
134
+ except (ValueError, RuntimeError) as e:
135
+ print("保存失败:", e)
136
+ return
137
+ self.saved.append((os.path.basename(path), self.rect))
138
+ print("模板 -> %s (%d×%d)" % (path, w, h))
139
+ self.rect = None
140
+
141
+ def loop(self):
142
+ cv2.namedWindow(self.win)
143
+ cv2.setMouseCallback(self.win, self.on_mouse)
144
+ while True:
145
+ disp = self.img.copy()
146
+
147
+ # 当前选区(绿色框)
148
+ if self.rect:
149
+ x0, y0, x1, y1 = self.rect
150
+ cv2.rectangle(disp, (x0, y0), (x1, y1), (0, 255, 0), 2)
151
+
152
+ # 已保存模板(橙色框 + 名称)
153
+ for name, (x0, y0, x1, y1) in self.saved:
154
+ cv2.rectangle(disp, (x0, y0), (x1, y1), (0, 200, 255), 1)
155
+ cv2.putText(disp, name, (x0, max(y0 - 5, 15)),
156
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 200, 255), 1)
157
+
158
+ info = "saved: %d | drag=select s=save n=clear q=quit" % len(self.saved)
159
+ cv2.putText(disp, info, (10, disp.shape[0] - 10),
160
+ cv2.FONT_HERSHEY_SIMPLEX, 0.55, (200, 200, 200), 1)
161
+
162
+ cv2.imshow(self.win, disp)
163
+ key = cv2.waitKey(30) & 0xFF
164
+
165
+ if key == ord("q"):
166
+ break
167
+ elif key == ord("s"):
168
+ self._save_current()
169
+ elif key == ord("n"):
170
+ self.rect = None
171
+ print("已清除当前选择")
172
+
173
+ cv2.destroyAllWindows()
174
+
175
+
176
+ def build_parser():
177
+ ap = argparse.ArgumentParser(
178
+ prog="crop-icon",
179
+ description="手动拖框截取 icon 模板:弹窗拖框选择区域并保存为模板图片")
180
+ ap.add_argument("--image", required=True, help="参考截图路径")
181
+ ap.add_argument("--out-dir", default="icon",
182
+ help="模板输出目录,相对当前工作目录解析(默认 ./icon)")
183
+ ap.add_argument("--no-resize", action="store_true",
184
+ help="不缩放到 1280×720,直接按原图操作")
185
+ return ap
186
+
187
+
188
+ def main(argv=None):
189
+ args = build_parser().parse_args(argv)
190
+
191
+ img = cv2.imread(args.image)
192
+ if img is None:
193
+ print("错误: 无法读取图片: %s" % args.image, file=sys.stderr)
194
+ return 1
195
+ if not args.no_resize:
196
+ img, _ = resize_to_720p(img)
197
+ print("图片已缩放至 1280×720")
198
+
199
+ out_dir = args.out_dir
200
+ if not os.path.isabs(out_dir):
201
+ out_dir = os.path.abspath(out_dir)
202
+ os.makedirs(out_dir, exist_ok=True)
203
+
204
+ cropper = _IconCropper(img, out_dir)
205
+ cropper.loop()
206
+
207
+ print("=" * 50)
208
+ print("完成: 共保存 %d 个模板到 %s" % (len(cropper.saved), out_dir))
209
+ return 0
210
+
211
+
212
+ if __name__ == "__main__":
213
+ sys.exit(main())
@@ -0,0 +1,87 @@
1
+ Metadata-Version: 2.4
2
+ Name: crop_icon
3
+ Version: 0.2.1
4
+ Summary: Interactive (mouse drag) command-line tool to crop icon templates from a screenshot
5
+ Author-email: Your Name <you@example.com>
6
+ License: MIT
7
+ Keywords: icon,crop,opencv,template-matching,screenshot
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Multimedia :: Graphics
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: opencv-python>=4.5
16
+ Requires-Dist: numpy
17
+ Provides-Extra: dev
18
+ Requires-Dist: build; extra == "dev"
19
+ Requires-Dist: twine; extra == "dev"
20
+ Dynamic: license-file
21
+
22
+ # crop-icon
23
+
24
+ 命令行 **icon 模板截取工具(手动拖框)**:弹窗显示参考截图,用鼠标拖框选择区域,
25
+ 按 `s` 后在终端输入文件名,即可把该区域保存为一张 icon 模板图片,供模板匹配 / 图标识别流程使用。
26
+ 仅负责截模板,不导出任何坐标 JSON。
27
+
28
+ ## 安装
29
+
30
+ ```bash
31
+ pip install crop-icon
32
+ ```
33
+
34
+ 安装时会自动下载依赖 `opencv-python`、`numpy`。
35
+
36
+ > 需要图形界面:Linux 上要有桌面或 X11 转发;若报缺少 `libGL`,
37
+ > 执行 `sudo apt install libgl1`。
38
+
39
+ ## 用法
40
+
41
+ ```bash
42
+ crop-icon --image reference.png --out-dir ./icon
43
+ ```
44
+
45
+ 弹出图像窗口后:
46
+
47
+ 1. **鼠标拖框** 选择要截取的区域;
48
+ 2. 按 **`s`**,在终端输入模板文件名(如 `nav_arrow`,不带扩展名会自动补 `.png`)→ 保存到 `out-dir`;
49
+ 3. 重复 1–2 截取多个模板;
50
+ 4. 按 **`q`** 退出。
51
+
52
+ 也可以用 `python -m crop_icon ...` 运行。
53
+
54
+ ### 按键
55
+
56
+ | 按键 | 作用 |
57
+ |---|---|
58
+ | 鼠标拖框 | 选择区域 |
59
+ | `s` | 保存当前选区为 icon 模板(终端输入文件名) |
60
+ | `n` | 清除当前选区 |
61
+ | `q` | 退出 |
62
+
63
+ ### 参数
64
+
65
+ | 参数 | 默认 | 说明 |
66
+ |---|---|---|
67
+ | `--image` | 必需 | 参考截图路径 |
68
+ | `--out-dir` | `./icon` | 模板输出目录(相对当前工作目录) |
69
+ | `--no-resize` | 关闭 | 不缩放到 1280×720,直接按原图操作 |
70
+
71
+ ### 说明
72
+
73
+ - 默认先把图缩放到 **1280×720** 再裁剪;用 `--no-resize` 关闭。
74
+ - 文件名不带扩展名时自动补 `.png`;名称可含中文(如 `激活icon.png`)。
75
+ - 选区小于 5×5 会被拒绝;超出图像边界会裁剪到边界。
76
+
77
+ ## 开发 / 发布
78
+
79
+ ```bash
80
+ pip install -e .[dev] # 可编辑安装 + build/twine
81
+ python -m build # 构建 dist/*.whl 与 *.tar.gz
82
+ twine upload dist/* # 发布到 PyPI
83
+ ```
84
+
85
+ ## 许可
86
+
87
+ MIT
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/crop_icon/__init__.py
5
+ src/crop_icon/__main__.py
6
+ src/crop_icon/cli.py
7
+ src/crop_icon.egg-info/PKG-INFO
8
+ src/crop_icon.egg-info/SOURCES.txt
9
+ src/crop_icon.egg-info/dependency_links.txt
10
+ src/crop_icon.egg-info/entry_points.txt
11
+ src/crop_icon.egg-info/requires.txt
12
+ src/crop_icon.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ crop-icon = crop_icon.cli:main
@@ -0,0 +1,6 @@
1
+ opencv-python>=4.5
2
+ numpy
3
+
4
+ [dev]
5
+ build
6
+ twine
@@ -0,0 +1 @@
1
+ crop_icon