Skip to content

correct-image-dimensions: Update Image Dimensions in VOC Annotations

The correct-image-dimensions command reads actual image dimensions from image files and updates corresponding Pascal VOC annotation files with the correct values. This is useful for fixing annotations that have incorrect or missing size information.

Usage

m3-download correct-image-dimensions ANNOTATION_DIR IMAGE_DIR [OPTIONS]

Parameters

  • ANNOTATION_DIR: Directory containing Pascal VOC annotation XML files
  • IMAGE_DIR: Directory containing corresponding images
  • --output-dir: (Optional) Directory to save corrected annotations; if omitted, original files are updated
  • --pretty-print: (Optional) Format the XML output with indentation (default: True)
  • --verbose, -v: (Optional) Display detailed logging information

Directory Separation

Annotation and image directories must be different. The command will exit with an error if they are the same.

Data Modification

If --output-dir is not specified, the original annotation files will be modified directly. Use the --output-dir option if you want to keep the original files unchanged.

How It Works

  1. The command associates annotation files with image files by matching their filename stems
  2. For each matched pair, it:
    • Reads the actual dimensions of the image using the imagesize library
    • Updates or creates the <size> element in the XML with correct <width> and <height> values
    • Writes the corrected XML to the output location
  3. A summary of updated and unchanged files is provided

File Matching

  • Images and annotation files are matched by filename (without extension)
  • Files with different extensions but the same name will be matched (e.g., image1.xml and image1.jpg)
  • Annotations without matching images are reported but not modified

Efficient Processing

The imagesize library is used to efficiently read dimensions without loading entire images into memory, making this command fast even with large image files.

Examples

Update annotations in-place:

m3-download correct-image-dimensions annotations/ images/

Save corrected annotations to a new directory:

m3-download correct-image-dimensions annotations/ images/ --output-dir corrected_annotations/

Enable verbose logging for troubleshooting:

m3-download correct-image-dimensions annotations/ images/ --verbose