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¶
Parameters¶
ANNOTATION_DIR: Directory containing Pascal VOC annotation XML filesIMAGE_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¶
- The command associates annotation files with image files by matching their filename stems
- For each matched pair, it:
- Reads the actual dimensions of the image using the
imagesizelibrary - Updates or creates the
<size>element in the XML with correct<width>and<height>values - Writes the corrected XML to the output location
- Reads the actual dimensions of the image using the
- 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.xmlandimage1.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:
Save corrected annotations to a new directory:
Enable verbose logging for troubleshooting: