Skip to content

Commit

Permalink
📝 feat(cropping): use original filename as prefix for cropped images (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CVHub520 committed Dec 14, 2024
1 parent 5c71187 commit 110e0f3
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions anylabeling/views/labeling/widgets/image_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,23 +346,35 @@ def _save_cropped_images(
def _crop_and_save(
self, image_file, label, points, save_path, label_to_count, shape_type
):
"""Crops the image to the specified region and saves it."""
"""
Crops the image to the specified region and saves it.
Uses original filename as prefix for cropped images.
"""
# Get original filename without extension
orig_filename = osp.splitext(osp.basename(image_file))[0]

# Calculate crop coordinates
x, y, w, h = cv2.boundingRect(points)
xmin, ymin, xmax, ymax = x, y, x + w, y + h

# Read and crop image
image = cv2.imread(image_file)
height, width = image.shape[:2]
xmin, ymin = max(0, xmin), max(0, ymin)
xmax, ymax = min(width, xmax), min(height, ymax)

crop_image = image[ymin:ymax, xmin:xmax]

# Create label directory
dst_path = osp.join(save_path, label)
os.makedirs(dst_path, exist_ok=True)

# Update counter and create filename
label_to_count[label] = label_to_count.get(label, 0) + 1
dst_file = osp.join(
dst_path, f"{label_to_count[label]}-{shape_type}.jpg"
dst_path,
f"{orig_filename}_{label_to_count[label]}-{shape_type}.jpg"
)

cv2.imwrite(dst_file, crop_image)

def _show_completion_message(self, save_path):
Expand Down

0 comments on commit 110e0f3

Please sign in to comment.