imagesize
The `imagesize` library is a pure Python module that efficiently extracts image dimensions, DPI, and other metadata (color depth, channel count, rotation) directly from image file headers, without needing to load the full image data. It supports a wide range of formats including BMP, PNG, JPEG, JPEG2000, GIF, TIFF, SVG, Netpbm, WebP, AVIF, HEIC, and HEIF. The current version is 2.0.0. It has a somewhat active release cadence, with recent updates in early March 2026.
Warnings
- breaking In version 2.0, `imagesize.get()` and `imagesize.getDPI()` no longer raise an exception on parsing failures. Instead, they now return `(-1, -1)`. Code that relied on exceptions for error handling will need to be updated to explicitly check for these return values.
- gotcha When using `imagesize.get()`, EXIF orientation metadata is applied by default for rotated JPEG/TIFF images, meaning the returned width and height reflect the visually oriented image. If you need the raw stored size, you must explicitly pass `exif_rotation=False`.
- gotcha The library primarily reads image *headers*. While it's generally robust, some severely corrupted images might still report dimensions if their headers are partially intact, even if the image data itself is unusable.
- gotcha For SVG images, `imagesize` only supports pixel dimensions and `viewBox` attributes. Percentage values for dimensions are not supported.
Install
-
pip install imagesize
Imports
- imagesize
import imagesize
- get
import imagesize width, height = imagesize.get('image.png') - getDPI
import imagesize xdpi, ydpi = imagesize.getDPI('image.png') - get_info
import imagesize info = imagesize.get_info('image.png')
Quickstart
import imagesize
import os
# Create a dummy image file for demonstration
with open('test_image.png', 'wb') as f:
f.write(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x0cIDATx\xda\xed\xc1\x01\x01\x00\x00\x00\xc2\xa0\xf7Om\x00\x00\x00\x00IEND\xaeB`\x82')
# Get basic image dimensions
width, height = imagesize.get('test_image.png')
print(f"Width: {width}, Height: {height}")
# Get DPI information (if available)
xdpi, ydpi = imagesize.getDPI('test_image.png')
print(f"X-DPI: {xdpi}, Y-DPI: {ydpi}")
# Get comprehensive image information
info = imagesize.get_info('test_image.png')
print(f"Image Info: Width={info.width}, Height={info.height}, Rotation={info.rotation}, X-DPI={info.xdpi}, Y-DPI={info.ydpi}, Colors={info.colors}, Channels={info.channels}")
# Clean up the dummy file
os.remove('test_image.png')