diff --git a/anylabeling/views/labeling/widgets/image_dialog.py b/anylabeling/views/labeling/widgets/image_dialog.py index d2c86c7..7e945ef 100644 --- a/anylabeling/views/labeling/widgets/image_dialog.py +++ b/anylabeling/views/labeling/widgets/image_dialog.py @@ -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):