imagesize
raw JSON → 2.0.0 verified Tue May 12 auth: no python install: verified
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.
pip install imagesize Common errors
error ModuleNotFoundError: No module named 'imagesize' ↓
cause The `imagesize` library has not been installed in the Python environment or the environment is not correctly activated.
fix
Install the library using pip:
pip install imagesize error AttributeError: module 'imagesize' has no attribute 'get_imagesize' ↓
cause Users are attempting to call an old function name (`get_imagesize`) that was deprecated in version 1.0.0 and removed in newer versions, instead of the current `imagesize.get`.
fix
Use the updated function name:
width, height = imagesize.get('image.jpg') error FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_image.jpg' ↓
cause The specified image file path does not exist or is incorrect, preventing `imagesize` from opening the file.
fix
Verify that the file path is correct and the file exists at that location, using an absolute path or ensuring the relative path is accurate.
error imagesize.UnidentifiedImageError: cannot identify image file <path/to/image.file> ↓
cause The provided file is either not a recognized image format, is corrupted, or its header information is malformed in a way that `imagesize` cannot parse.
fix
Ensure the file is a valid, supported image format (e.g., JPEG, PNG, GIF) and not corrupted. If the file is expected to be valid, verify its integrity or try opening it with an image viewer.
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. ↓
fix Update error handling logic to check for `(-1, -1)` as a return value from `imagesize.get()` and `imagesize.getDPI()` instead of catching exceptions.
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`. ↓
fix If the raw, unrotated dimensions are needed, call `imagesize.get(filepath, 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. ↓
fix Consider adding additional image integrity checks if your application requires highly reliable image data beyond just header parsing.
gotcha For SVG images, `imagesize` only supports pixel dimensions and `viewBox` attributes. Percentage values for dimensions are not supported. ↓
fix Ensure SVG assets are defined with explicit pixel dimensions or a `viewBox` for reliable parsing with `imagesize`.
breaking The `imagesize.get_info()` function has been removed in version 2.0. Code that relied on this function will raise an AttributeError. Users should migrate to `imagesize.get()` for dimensions and `imagesize.getDPI()` for DPI information. ↓
fix Update code that uses `imagesize.get_info()` to instead use `imagesize.get(filepath)` for dimensions or `imagesize.getDPI(filepath)` for DPI. The API has been refactored.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.13s 17.8M
3.10 alpine (musl) - - 0.14s 17.8M
3.10 slim (glibc) wheel 1.4s 0.09s 18M
3.10 slim (glibc) - - 0.09s 18M
3.11 alpine (musl) wheel - 0.19s 19.7M
3.11 alpine (musl) - - 0.21s 19.7M
3.11 slim (glibc) wheel 1.5s 0.16s 20M
3.11 slim (glibc) - - 0.15s 20M
3.12 alpine (musl) wheel - 0.15s 11.6M
3.12 alpine (musl) - - 0.16s 11.6M
3.12 slim (glibc) wheel 1.4s 0.15s 12M
3.12 slim (glibc) - - 0.15s 12M
3.13 alpine (musl) wheel - 0.14s 11.3M
3.13 alpine (musl) - - 0.15s 11.2M
3.13 slim (glibc) wheel 1.4s 0.17s 12M
3.13 slim (glibc) - - 0.14s 12M
3.9 alpine (musl) wheel - 0.04s 17.3M
3.9 alpine (musl) - - 0.05s 17.3M
3.9 slim (glibc) wheel 1.7s 0.04s 18M
3.9 slim (glibc) - - 0.04s 18M
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 last tested: 2026-04-24
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')