回退到直接截图
This commit is contained in:
1 parent
478ae69052
commit
7b93413af7
41 files changed
+380
-64
No files matched your search
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Annotate an image with a bounding box and a click point.
|
||||
Usage:
|
||||
python scripts/annotate_image.py "<image_path>" x1 y1 x2 y2 click_x click_y
|
||||
Saves annotated image next to original with suffix "_annotated.png".
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
except Exception:
|
||||
print("Pillow not installed. Install with: pip install pillow")
|
||||
raise
|
||||
|
||||
|
||||
def annotate_image(src_path: Path, bbox, click_point, out_path: Path) -> None:
|
||||
im = Image.open(src_path).convert("RGBA")
|
||||
draw = ImageDraw.Draw(im)
|
||||
x1, y1, x2, y2 = bbox
|
||||
# rectangle
|
||||
for i in range(4):
|
||||
draw.rectangle((x1 - i, y1 - i, x2 + i, y2 + i), outline=(255, 0, 0, 200))
|
||||
# click point (circle)
|
||||
cx, cy = click_point
|
||||
r = max(6, int(min(im.size) * 0.01))
|
||||
draw.ellipse([cx - r, cy - r, cx + r, cy + r], fill=(0, 128, 255, 200))
|
||||
# label
|
||||
try:
|
||||
font = ImageFont.load_default()
|
||||
except Exception:
|
||||
font = None
|
||||
label = f"bbox: ({x1},{y1})-({x2},{y2}) click: ({cx},{cy})"
|
||||
text_pos = (x1, max(0, y1 - 14))
|
||||
draw.rectangle((text_pos[0], text_pos[1], text_pos[0] + 400, text_pos[1] + 14), fill=(0, 0, 0, 120))
|
||||
draw.text(text_pos, label, fill=(255, 255, 255, 255), font=font)
|
||||
im.save(out_path)
|
||||
print(f"Saved annotated image: {out_path}")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 8:
|
||||
print("Usage: python scripts/annotate_image.py <image_path> x1 y1 x2 y2 click_x click_y")
|
||||
sys.exit(2)
|
||||
src = Path(sys.argv[1])
|
||||
if not src.exists():
|
||||
print("Source image not found (path may contain unicode or file missing)")
|
||||
sys.exit(1)
|
||||
try:
|
||||
x1 = int(sys.argv[2]); y1 = int(sys.argv[3]); x2 = int(sys.argv[4]); y2 = int(sys.argv[5])
|
||||
cx = int(sys.argv[6]); cy = int(sys.argv[7])
|
||||
except Exception as e:
|
||||
print("Invalid coordinates:", e)
|
||||
sys.exit(1)
|
||||
out = src.with_name(src.stem + "_annotated.png")
|
||||
annotate_image(src, (x1, y1, x2, y2), (cx, cy), out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
Reference in new issue
Block a user