pattyeng 1.0.4 → 1.0.6
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.
- package/README.md +98 -44
- package/bin/pattyeng.js +20 -14
- package/lib/skills.js +170 -0
- package/lib/upgrade.js +46 -0
- package/package.json +5 -3
- package/skills/.system/.codex-system-skills.marker +1 -0
- package/skills/.system/imagegen/LICENSE.txt +201 -0
- package/skills/.system/imagegen/SKILL.md +356 -0
- package/skills/.system/imagegen/agents/openai.yaml +6 -0
- package/skills/.system/imagegen/assets/imagegen-small.svg +5 -0
- package/skills/.system/imagegen/assets/imagegen.png +0 -0
- package/skills/.system/imagegen/references/cli.md +242 -0
- package/skills/.system/imagegen/references/codex-network.md +33 -0
- package/skills/.system/imagegen/references/image-api.md +90 -0
- package/skills/.system/imagegen/references/prompting.md +118 -0
- package/skills/.system/imagegen/references/sample-prompts.md +433 -0
- package/skills/.system/imagegen/scripts/image_gen.py +995 -0
- package/skills/.system/imagegen/scripts/remove_chroma_key.py +440 -0
- package/skills/.system/openai-docs/LICENSE.txt +201 -0
- package/skills/.system/openai-docs/SKILL.md +167 -0
- package/skills/.system/openai-docs/agents/openai.yaml +14 -0
- package/skills/.system/openai-docs/assets/openai-small.svg +3 -0
- package/skills/.system/openai-docs/assets/openai.png +0 -0
- package/skills/.system/openai-docs/references/latest-model.md +37 -0
- package/skills/.system/openai-docs/references/prompting-guide.md +244 -0
- package/skills/.system/openai-docs/references/upgrade-guide.md +181 -0
- package/skills/.system/openai-docs/scripts/fetch-codex-manual.mjs +598 -0
- package/skills/.system/openai-docs/scripts/resolve-latest-model-info.js +147 -0
- package/skills/.system/plugin-creator/SKILL.md +243 -0
- package/skills/.system/plugin-creator/agents/openai.yaml +6 -0
- package/skills/.system/plugin-creator/assets/plugin-creator-small.svg +3 -0
- package/skills/.system/plugin-creator/assets/plugin-creator.png +0 -0
- package/skills/.system/plugin-creator/references/installing-and-updating.md +143 -0
- package/skills/.system/plugin-creator/references/plugin-json-spec.md +194 -0
- package/skills/.system/plugin-creator/scripts/create_basic_plugin.py +324 -0
- package/skills/.system/plugin-creator/scripts/read_marketplace_name.py +48 -0
- package/skills/.system/plugin-creator/scripts/update_plugin_cachebuster.py +78 -0
- package/skills/.system/plugin-creator/scripts/validate_plugin.py +586 -0
- package/skills/.system/skill-creator/SKILL.md +416 -0
- package/skills/.system/skill-creator/agents/openai.yaml +5 -0
- package/skills/.system/skill-creator/assets/skill-creator-small.svg +3 -0
- package/skills/.system/skill-creator/assets/skill-creator.png +0 -0
- package/skills/.system/skill-creator/license.txt +202 -0
- package/skills/.system/skill-creator/references/openai_yaml.md +49 -0
- package/skills/.system/skill-creator/scripts/generate_openai_yaml.py +226 -0
- package/skills/.system/skill-creator/scripts/init_skill.py +400 -0
- package/skills/.system/skill-creator/scripts/quick_validate.py +101 -0
- package/skills/.system/skill-installer/LICENSE.txt +202 -0
- package/skills/.system/skill-installer/SKILL.md +58 -0
- package/skills/.system/skill-installer/agents/openai.yaml +5 -0
- package/skills/.system/skill-installer/assets/skill-installer-small.svg +3 -0
- package/skills/.system/skill-installer/assets/skill-installer.png +0 -0
- package/skills/.system/skill-installer/scripts/github_utils.py +21 -0
- package/skills/.system/skill-installer/scripts/install-skill-from-github.py +308 -0
- package/skills/.system/skill-installer/scripts/list-skills.py +107 -0
- package/skills/cap/SKILL.md +77 -0
- package/skills/caveman/SKILL.md +49 -0
- package/skills/code-review/SKILL.md +46 -0
- package/skills/codebook/SKILL.md +184 -0
- package/skills/doc-review/SKILL.md +147 -0
- package/skills/feature-research/SKILL.md +34 -0
- package/skills/file-op/SKILL.md +116 -0
- package/skills/grill-me/SKILL.md +10 -0
- package/skills/grill-with-docs/ADR-FORMAT.md +47 -0
- package/skills/grill-with-docs/CONTEXT-FORMAT.md +60 -0
- package/skills/grill-with-docs/SKILL.md +88 -0
- package/skills/handoff/SKILL.md +17 -0
- package/skills/improve-codebase/SKILL.md +79 -0
- package/skills/linear/SKILL.md +107 -0
- package/skills/next-step/SKILL.md +61 -0
- package/skills/no-reinvent/SKILL.md +131 -0
- package/skills/no-wall/SKILL.md +146 -0
- package/skills/online/SKILL.md +54 -0
- package/skills/plan-review/SKILL.md +55 -0
- package/skills/professional/SKILL.md +111 -0
- package/skills/promise-tracking/SKILL.md +131 -0
- package/skills/prove/SKILL.md +135 -0
- package/skills/read-before-write/SKILL.md +123 -0
- package/skills/review/SKILL.md +78 -0
- package/skills/review-mp/SKILL.md +78 -0
- package/skills/scope-boundary/SKILL.md +121 -0
- package/skills/semble-search/SKILL.md +40 -0
- package/skills/sitrep/SKILL.md +126 -0
- package/skills/structure-code/SKILL.md +116 -0
- package/skills/surface-assumptions/SKILL.md +139 -0
- package/skills/teach/GLOSSARY-FORMAT.md +35 -0
- package/skills/teach/LEARNING-RECORD-FORMAT.md +46 -0
- package/skills/teach/MISSION-FORMAT.md +31 -0
- package/skills/teach/RESOURCES-FORMAT.md +32 -0
- package/skills/teach/SKILL.md +131 -0
- package/skills/team-protocol/SKILL.md +172 -0
- package/skills/tech-lead/SKILL.md +332 -0
- package/skills/to-issues/SKILL.md +83 -0
- package/skills/to-prd/SKILL.md +74 -0
- package/skills/vectorbt-expert/SKILL.md +253 -0
- package/skills/verify-done/SKILL.md +128 -0
- package/skills/work-log/SKILL.md +141 -0
- package/skills/work-log/template.html +496 -0
- package/skills/zoom-out/SKILL.md +7 -0
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Remove a solid chroma-key background from an image.
|
|
3
|
+
|
|
4
|
+
This helper supports the imagegen skill's built-in-first transparent workflow:
|
|
5
|
+
generate an image on a flat key color, then convert that key color to alpha.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
from io import BytesIO
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
import re
|
|
14
|
+
from statistics import median
|
|
15
|
+
import sys
|
|
16
|
+
from typing import Tuple
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Color = Tuple[int, int, int]
|
|
20
|
+
KEY_DOMINANCE_THRESHOLD = 16.0
|
|
21
|
+
ALPHA_NOISE_FLOOR = 8
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _die(message: str, code: int = 1) -> None:
|
|
25
|
+
print(f"Error: {message}", file=sys.stderr)
|
|
26
|
+
raise SystemExit(code)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _dependency_hint(package: str) -> str:
|
|
30
|
+
return (
|
|
31
|
+
"Activate the repo-selected environment first, then install it with "
|
|
32
|
+
f"`uv pip install {package}`. If this repo uses a local virtualenv, start with "
|
|
33
|
+
"`source .venv/bin/activate`; otherwise use this repo's configured shared fallback "
|
|
34
|
+
"environment."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _load_pillow():
|
|
39
|
+
try:
|
|
40
|
+
from PIL import Image, ImageFilter
|
|
41
|
+
except ImportError:
|
|
42
|
+
_die(f"Pillow is required for chroma-key removal. {_dependency_hint('pillow')}")
|
|
43
|
+
return Image, ImageFilter
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _parse_key_color(raw: str) -> Color:
|
|
47
|
+
value = raw.strip()
|
|
48
|
+
match = re.fullmatch(r"#?([0-9a-fA-F]{6})", value)
|
|
49
|
+
if not match:
|
|
50
|
+
_die("key color must be a hex RGB value like #00ff00.")
|
|
51
|
+
hex_value = match.group(1)
|
|
52
|
+
return (
|
|
53
|
+
int(hex_value[0:2], 16),
|
|
54
|
+
int(hex_value[2:4], 16),
|
|
55
|
+
int(hex_value[4:6], 16),
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _validate_args(args: argparse.Namespace) -> None:
|
|
60
|
+
if args.tolerance < 0 or args.tolerance > 255:
|
|
61
|
+
_die("--tolerance must be between 0 and 255.")
|
|
62
|
+
if args.transparent_threshold < 0 or args.transparent_threshold > 255:
|
|
63
|
+
_die("--transparent-threshold must be between 0 and 255.")
|
|
64
|
+
if args.opaque_threshold < 0 or args.opaque_threshold > 255:
|
|
65
|
+
_die("--opaque-threshold must be between 0 and 255.")
|
|
66
|
+
if args.soft_matte and args.transparent_threshold >= args.opaque_threshold:
|
|
67
|
+
_die("--transparent-threshold must be lower than --opaque-threshold.")
|
|
68
|
+
if args.edge_feather < 0 or args.edge_feather > 64:
|
|
69
|
+
_die("--edge-feather must be between 0 and 64.")
|
|
70
|
+
if args.edge_contract < 0 or args.edge_contract > 16:
|
|
71
|
+
_die("--edge-contract must be between 0 and 16.")
|
|
72
|
+
|
|
73
|
+
src = Path(args.input)
|
|
74
|
+
if not src.exists():
|
|
75
|
+
_die(f"Input image not found: {src}")
|
|
76
|
+
|
|
77
|
+
out = Path(args.out)
|
|
78
|
+
if out.exists() and not args.force:
|
|
79
|
+
_die(f"Output already exists: {out} (use --force to overwrite)")
|
|
80
|
+
|
|
81
|
+
if out.suffix.lower() not in {".png", ".webp"}:
|
|
82
|
+
_die("--out must end in .png or .webp so the alpha channel is preserved.")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _channel_distance(a: Color, b: Color) -> int:
|
|
86
|
+
return max(abs(a[0] - b[0]), abs(a[1] - b[1]), abs(a[2] - b[2]))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _clamp_channel(value: float) -> int:
|
|
90
|
+
return max(0, min(255, int(round(value))))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _smoothstep(value: float) -> float:
|
|
94
|
+
value = max(0.0, min(1.0, value))
|
|
95
|
+
return value * value * (3.0 - 2.0 * value)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _soft_alpha(distance: int, transparent_threshold: float, opaque_threshold: float) -> int:
|
|
99
|
+
if distance <= transparent_threshold:
|
|
100
|
+
return 0
|
|
101
|
+
if distance >= opaque_threshold:
|
|
102
|
+
return 255
|
|
103
|
+
ratio = (float(distance) - transparent_threshold) / (
|
|
104
|
+
opaque_threshold - transparent_threshold
|
|
105
|
+
)
|
|
106
|
+
return _clamp_channel(255.0 * _smoothstep(ratio))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _dominance_alpha(rgb: Color, key: Color) -> int:
|
|
110
|
+
spill_channels = _spill_channels(key)
|
|
111
|
+
if not spill_channels:
|
|
112
|
+
return 255
|
|
113
|
+
|
|
114
|
+
channels = [float(value) for value in rgb]
|
|
115
|
+
non_spill = [idx for idx in range(3) if idx not in spill_channels]
|
|
116
|
+
key_strength = (
|
|
117
|
+
min(channels[idx] for idx in spill_channels)
|
|
118
|
+
if len(spill_channels) > 1
|
|
119
|
+
else channels[spill_channels[0]]
|
|
120
|
+
)
|
|
121
|
+
non_key_strength = max((channels[idx] for idx in non_spill), default=0.0)
|
|
122
|
+
dominance = key_strength - non_key_strength
|
|
123
|
+
if dominance <= 0:
|
|
124
|
+
return 255
|
|
125
|
+
|
|
126
|
+
denominator = max(1.0, float(max(key)) - non_key_strength)
|
|
127
|
+
alpha = 1.0 - min(1.0, dominance / denominator)
|
|
128
|
+
return _clamp_channel(alpha * 255.0)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _spill_channels(key: Color) -> list[int]:
|
|
132
|
+
key_max = max(key)
|
|
133
|
+
if key_max < 128:
|
|
134
|
+
return []
|
|
135
|
+
return [idx for idx, value in enumerate(key) if value >= key_max - 16 and value >= 128]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _key_channel_dominance(rgb: Color, key: Color) -> float:
|
|
139
|
+
spill_channels = _spill_channels(key)
|
|
140
|
+
if not spill_channels:
|
|
141
|
+
return 0.0
|
|
142
|
+
|
|
143
|
+
channels = [float(value) for value in rgb]
|
|
144
|
+
non_spill = [idx for idx in range(3) if idx not in spill_channels]
|
|
145
|
+
key_strength = (
|
|
146
|
+
min(channels[idx] for idx in spill_channels)
|
|
147
|
+
if len(spill_channels) > 1
|
|
148
|
+
else channels[spill_channels[0]]
|
|
149
|
+
)
|
|
150
|
+
non_key_strength = max((channels[idx] for idx in non_spill), default=0.0)
|
|
151
|
+
return key_strength - non_key_strength
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _looks_key_colored(rgb: Color, key: Color, distance: int) -> bool:
|
|
155
|
+
if distance <= 32:
|
|
156
|
+
return True
|
|
157
|
+
|
|
158
|
+
spill_channels = _spill_channels(key)
|
|
159
|
+
if not spill_channels:
|
|
160
|
+
return True
|
|
161
|
+
|
|
162
|
+
return _key_channel_dominance(rgb, key) >= KEY_DOMINANCE_THRESHOLD
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _cleanup_spill(rgb: Color, key: Color, alpha: int = 255) -> Color:
|
|
166
|
+
if alpha >= 252:
|
|
167
|
+
return rgb
|
|
168
|
+
|
|
169
|
+
spill_channels = _spill_channels(key)
|
|
170
|
+
if not spill_channels:
|
|
171
|
+
return rgb
|
|
172
|
+
|
|
173
|
+
channels = [float(value) for value in rgb]
|
|
174
|
+
non_spill = [idx for idx in range(3) if idx not in spill_channels]
|
|
175
|
+
if non_spill:
|
|
176
|
+
anchor = max(channels[idx] for idx in non_spill)
|
|
177
|
+
cap = max(0.0, anchor - 1.0)
|
|
178
|
+
for idx in spill_channels:
|
|
179
|
+
if channels[idx] > cap:
|
|
180
|
+
channels[idx] = cap
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
_clamp_channel(channels[0]),
|
|
184
|
+
_clamp_channel(channels[1]),
|
|
185
|
+
_clamp_channel(channels[2]),
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _apply_alpha_to_image(
|
|
190
|
+
image,
|
|
191
|
+
*,
|
|
192
|
+
key: Color,
|
|
193
|
+
tolerance: int,
|
|
194
|
+
spill_cleanup: bool,
|
|
195
|
+
soft_matte: bool,
|
|
196
|
+
transparent_threshold: float,
|
|
197
|
+
opaque_threshold: float,
|
|
198
|
+
) -> int:
|
|
199
|
+
pixels = image.load()
|
|
200
|
+
width, height = image.size
|
|
201
|
+
transparent = 0
|
|
202
|
+
|
|
203
|
+
for y in range(height):
|
|
204
|
+
for x in range(width):
|
|
205
|
+
red, green, blue, alpha = pixels[x, y]
|
|
206
|
+
rgb = (red, green, blue)
|
|
207
|
+
distance = _channel_distance(rgb, key)
|
|
208
|
+
key_like = _looks_key_colored(rgb, key, distance)
|
|
209
|
+
output_alpha = (
|
|
210
|
+
min(
|
|
211
|
+
_soft_alpha(distance, transparent_threshold, opaque_threshold),
|
|
212
|
+
_dominance_alpha(rgb, key),
|
|
213
|
+
)
|
|
214
|
+
if soft_matte and key_like
|
|
215
|
+
else (0 if distance <= tolerance else 255)
|
|
216
|
+
)
|
|
217
|
+
output_alpha = int(round(output_alpha * (alpha / 255.0)))
|
|
218
|
+
if 0 < output_alpha <= ALPHA_NOISE_FLOOR:
|
|
219
|
+
output_alpha = 0
|
|
220
|
+
|
|
221
|
+
if output_alpha == 0:
|
|
222
|
+
pixels[x, y] = (0, 0, 0, 0)
|
|
223
|
+
transparent += 1
|
|
224
|
+
continue
|
|
225
|
+
|
|
226
|
+
if spill_cleanup and key_like:
|
|
227
|
+
red, green, blue = _cleanup_spill(rgb, key, output_alpha)
|
|
228
|
+
pixels[x, y] = (red, green, blue, output_alpha)
|
|
229
|
+
|
|
230
|
+
return transparent
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _contract_alpha(image, pixels: int):
|
|
234
|
+
if pixels == 0:
|
|
235
|
+
return image
|
|
236
|
+
|
|
237
|
+
_, ImageFilter = _load_pillow()
|
|
238
|
+
alpha = image.getchannel("A")
|
|
239
|
+
for _ in range(pixels):
|
|
240
|
+
alpha = alpha.filter(ImageFilter.MinFilter(3))
|
|
241
|
+
image.putalpha(alpha)
|
|
242
|
+
return image
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _apply_edge_feather(image, radius: float):
|
|
246
|
+
if radius == 0:
|
|
247
|
+
return image
|
|
248
|
+
|
|
249
|
+
_, ImageFilter = _load_pillow()
|
|
250
|
+
alpha = image.getchannel("A")
|
|
251
|
+
alpha = alpha.filter(ImageFilter.GaussianBlur(radius=radius))
|
|
252
|
+
image.putalpha(alpha)
|
|
253
|
+
return image
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _encode_image(image, output_format: str) -> bytes:
|
|
257
|
+
out = BytesIO()
|
|
258
|
+
image.save(out, format=output_format.upper())
|
|
259
|
+
return out.getvalue()
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _alpha_counts(image) -> tuple[int, int, int]:
|
|
263
|
+
pixels = image.load()
|
|
264
|
+
width, height = image.size
|
|
265
|
+
total = 0
|
|
266
|
+
transparent = 0
|
|
267
|
+
partial = 0
|
|
268
|
+
|
|
269
|
+
for y in range(height):
|
|
270
|
+
for x in range(width):
|
|
271
|
+
alpha = pixels[x, y][3]
|
|
272
|
+
total += 1
|
|
273
|
+
if alpha == 0:
|
|
274
|
+
transparent += 1
|
|
275
|
+
elif alpha < 255:
|
|
276
|
+
partial += 1
|
|
277
|
+
|
|
278
|
+
return total, transparent, partial
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _sample_border_key(image, mode: str) -> Color:
|
|
282
|
+
width, height = image.size
|
|
283
|
+
pixels = image.load()
|
|
284
|
+
samples: list[Color] = []
|
|
285
|
+
|
|
286
|
+
if mode == "corners":
|
|
287
|
+
patch = max(1, min(width, height, 12))
|
|
288
|
+
boxes = [
|
|
289
|
+
(0, 0, patch, patch),
|
|
290
|
+
(width - patch, 0, width, patch),
|
|
291
|
+
(0, height - patch, patch, height),
|
|
292
|
+
(width - patch, height - patch, width, height),
|
|
293
|
+
]
|
|
294
|
+
for left, top, right, bottom in boxes:
|
|
295
|
+
for y in range(top, bottom):
|
|
296
|
+
for x in range(left, right):
|
|
297
|
+
red, green, blue = pixels[x, y][:3]
|
|
298
|
+
samples.append((red, green, blue))
|
|
299
|
+
else:
|
|
300
|
+
band = max(1, min(width, height, 6))
|
|
301
|
+
step = max(1, min(width, height) // 256)
|
|
302
|
+
for x in range(0, width, step):
|
|
303
|
+
for y in range(band):
|
|
304
|
+
red, green, blue = pixels[x, y][:3]
|
|
305
|
+
samples.append((red, green, blue))
|
|
306
|
+
red, green, blue = pixels[x, height - 1 - y][:3]
|
|
307
|
+
samples.append((red, green, blue))
|
|
308
|
+
for y in range(0, height, step):
|
|
309
|
+
for x in range(band):
|
|
310
|
+
red, green, blue = pixels[x, y][:3]
|
|
311
|
+
samples.append((red, green, blue))
|
|
312
|
+
red, green, blue = pixels[width - 1 - x, y][:3]
|
|
313
|
+
samples.append((red, green, blue))
|
|
314
|
+
|
|
315
|
+
if not samples:
|
|
316
|
+
_die("Could not sample background key color from image border.")
|
|
317
|
+
|
|
318
|
+
return (
|
|
319
|
+
int(round(median(sample[0] for sample in samples))),
|
|
320
|
+
int(round(median(sample[1] for sample in samples))),
|
|
321
|
+
int(round(median(sample[2] for sample in samples))),
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _remove_chroma_key(args: argparse.Namespace) -> None:
|
|
326
|
+
Image, _ = _load_pillow()
|
|
327
|
+
src = Path(args.input)
|
|
328
|
+
out = Path(args.out)
|
|
329
|
+
|
|
330
|
+
with Image.open(src) as image:
|
|
331
|
+
rgba = image.convert("RGBA")
|
|
332
|
+
key = (
|
|
333
|
+
_sample_border_key(rgba, args.auto_key)
|
|
334
|
+
if args.auto_key != "none"
|
|
335
|
+
else _parse_key_color(args.key_color)
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
transparent = _apply_alpha_to_image(
|
|
339
|
+
rgba,
|
|
340
|
+
key=key,
|
|
341
|
+
tolerance=args.tolerance,
|
|
342
|
+
spill_cleanup=args.spill_cleanup,
|
|
343
|
+
soft_matte=args.soft_matte,
|
|
344
|
+
transparent_threshold=args.transparent_threshold,
|
|
345
|
+
opaque_threshold=args.opaque_threshold,
|
|
346
|
+
)
|
|
347
|
+
rgba = _contract_alpha(rgba, args.edge_contract)
|
|
348
|
+
rgba = _apply_edge_feather(rgba, args.edge_feather)
|
|
349
|
+
|
|
350
|
+
total, transparent_after, partial_after = _alpha_counts(rgba)
|
|
351
|
+
|
|
352
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
353
|
+
output_format = "PNG" if out.suffix.lower() == ".png" else "WEBP"
|
|
354
|
+
out.write_bytes(_encode_image(rgba, output_format))
|
|
355
|
+
|
|
356
|
+
print(f"Wrote {out}")
|
|
357
|
+
print(f"Key color: #{key[0]:02x}{key[1]:02x}{key[2]:02x}")
|
|
358
|
+
print(f"Transparent pixels: {transparent_after}/{total}")
|
|
359
|
+
print(f"Partially transparent pixels: {partial_after}/{total}")
|
|
360
|
+
if transparent == 0:
|
|
361
|
+
print("Warning: no pixels matched the key color before feathering.", file=sys.stderr)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
365
|
+
parser = argparse.ArgumentParser(
|
|
366
|
+
description="Remove a solid chroma-key background and write an image with alpha."
|
|
367
|
+
)
|
|
368
|
+
parser.add_argument("--input", required=True, help="Input image path.")
|
|
369
|
+
parser.add_argument("--out", required=True, help="Output .png or .webp path.")
|
|
370
|
+
parser.add_argument(
|
|
371
|
+
"--key-color",
|
|
372
|
+
default="#00ff00",
|
|
373
|
+
help="Hex RGB key color to remove, for example #00ff00.",
|
|
374
|
+
)
|
|
375
|
+
parser.add_argument(
|
|
376
|
+
"--tolerance",
|
|
377
|
+
type=int,
|
|
378
|
+
default=12,
|
|
379
|
+
help="Hard-key per-channel tolerance for matching the key color, 0-255.",
|
|
380
|
+
)
|
|
381
|
+
parser.add_argument(
|
|
382
|
+
"--auto-key",
|
|
383
|
+
choices=["none", "corners", "border"],
|
|
384
|
+
default="none",
|
|
385
|
+
help="Sample the key color from image corners or border instead of --key-color.",
|
|
386
|
+
)
|
|
387
|
+
parser.add_argument(
|
|
388
|
+
"--soft-matte",
|
|
389
|
+
action="store_true",
|
|
390
|
+
help="Use a smooth alpha ramp between transparent and opaque thresholds.",
|
|
391
|
+
)
|
|
392
|
+
parser.add_argument(
|
|
393
|
+
"--transparent-threshold",
|
|
394
|
+
type=float,
|
|
395
|
+
default=12.0,
|
|
396
|
+
help="Soft-matte distance at or below which pixels become fully transparent.",
|
|
397
|
+
)
|
|
398
|
+
parser.add_argument(
|
|
399
|
+
"--opaque-threshold",
|
|
400
|
+
type=float,
|
|
401
|
+
default=96.0,
|
|
402
|
+
help="Soft-matte distance at or above which pixels become fully opaque.",
|
|
403
|
+
)
|
|
404
|
+
parser.add_argument(
|
|
405
|
+
"--edge-feather",
|
|
406
|
+
type=float,
|
|
407
|
+
default=0.0,
|
|
408
|
+
help="Optional alpha blur radius for softened edges, 0-64.",
|
|
409
|
+
)
|
|
410
|
+
parser.add_argument(
|
|
411
|
+
"--edge-contract",
|
|
412
|
+
type=int,
|
|
413
|
+
default=0,
|
|
414
|
+
help="Shrink the visible alpha matte by this many pixels before feathering.",
|
|
415
|
+
)
|
|
416
|
+
parser.add_argument(
|
|
417
|
+
"--spill-cleanup",
|
|
418
|
+
dest="spill_cleanup",
|
|
419
|
+
action="store_true",
|
|
420
|
+
help="Reduce obvious key-color spill on opaque pixels.",
|
|
421
|
+
)
|
|
422
|
+
parser.add_argument(
|
|
423
|
+
"--despill",
|
|
424
|
+
dest="spill_cleanup",
|
|
425
|
+
action="store_true",
|
|
426
|
+
help="Alias for --spill-cleanup; decontaminate key-color edge spill.",
|
|
427
|
+
)
|
|
428
|
+
parser.add_argument("--force", action="store_true", help="Overwrite an existing output file.")
|
|
429
|
+
return parser
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def main() -> None:
|
|
433
|
+
parser = _build_parser()
|
|
434
|
+
args = parser.parse_args()
|
|
435
|
+
_validate_args(args)
|
|
436
|
+
_remove_chroma_key(args)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
if __name__ == "__main__":
|
|
440
|
+
main()
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf of
|
|
171
|
+
any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don\'t include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|