Skip to main content
https://github.com/shibukawa/imagesize_py/actions/workflows/ci.yml/badge.svg PyPI version Supported Python versions License

This module analyzes JPEG/JPEG 2000/PNG/GIF/TIFF/SVG/Netpbm/WebP/BMP/AVIF/HEIC/HEIF image headers and returns image size, DPI, and related metadata.

import imagesize

width, height = imagesize.get("test.png")
print(width, height)

xdpi, ydpi = imagesize.getDPI("test.png")
print(xdpi, ydpi)

info = imagesize.get_info("test.png")
print(info.width, info.height, info.rotation, info.xdpi, info.ydpi, info.colors, info.channels)

This module is a pure Python module. You can use file like object like file or something like io.BytesIO.

Supported Python versions: 3.10-3.15

Installation

pip install imagesize

For local development setup:

python -m venv .venv
source .venv/bin/activate
pip install -e .

Version 2.0 migration notes

Version 2.0 includes the following updates:

  • Added/expanded support for BMP.

  • Added support for AVIF.

  • Added support for HEIC/HEIF.

  • imagesize.get_info() now returns richer metadata including color depth, channel count, and rotation information.

  • Improved EXIF orientation handling so JPEG, TIFF, AVIF, and HEIC/HEIF return sizes that correctly reflect EXIF rotation metadata.

  • Added type hints for the public API and related input/output types.

Backward incompatible behavior in 2.0:

  • imagesize.get() now returns (-1, -1) when parsing fails.

  • imagesize.getDPI() now returns (-1, -1) when parsing fails.

If your existing code relied on exceptions during parse failures, update it to explicitly check return values.

API

  • imagesize.get(filepath: FileInput, *, exif_rotation: bool = True) -> tuple[int, int]

    Returns image size as (width, height). By default, orientation metadata is applied for rotated JPEG/TIFF/AVIF/HEIF images; pass exif_rotation=False to get the stored size as-is. On parsing errors it returns (-1, -1).

  • imagesize.getDPI(filepath: FileInput) -> tuple[int, int]

    Returns image DPI as (xdpi, ydpi). On parsing errors it returns (-1, -1).

  • imagesize.get_info(filepath: FileInput, *, size: bool = True, dpi: bool = True, colors: bool = True, exif_rotation: bool = True, channels: bool = True) -> ImageInfo

    Returns an ImageInfo named tuple with width, height, rotation, xdpi, ydpi, colors and channels fields. rotation contains orientation metadata (e.g. EXIF Orientation tag, or -1 when unavailable).

HTTP and HTTPS URLs are accepted as input. The library uses HTTP byte-range requests automatically so that pixel data does not need to be downloaded. If a server does not support range requests, it transparently falls back to one full download for compatibility. The first request is limited to 8 KiB; later random access expands cached regions in 64 KiB blocks only when more metadata is needed.

Benchmark

Only headers and the metadata needed for the requested result are read. Pixel payloads are skipped with seek() locally and HTTP Range requests remotely. Run the included benchmark with:

python bench.py --number 10000

The report includes elapsed time and the number of bytes read for PNG, JPEG, TIFF, GIF, JPEG2000, AVIF, and HEIF. It also simulates an 8 MiB remote AVIF and reports the number of Range requests and transferred bytes.

Development

Run test with the following command:

python -m unittest

License

MIT License

Thanks

I referred to the following code:

I use sample image from here:

Thank you for feedback:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

imagesize-2.0.1.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

imagesize-2.0.1-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file imagesize-2.0.1.tar.gz.

File metadata

  • Download URL: imagesize-2.0.1.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.0

File hashes

Hashes for imagesize-2.0.1.tar.gz
Algorithm Hash digest
SHA256 b2ba6a4dea487a7ebcd53248d3476aca449d30db12a2dde5e0c5ca9624fd77e5
MD5 92f3abd491aaf33b4482cb5d9e94e972
BLAKE2b-256 fb5e513ff06670c84e7b9887c1fdf61b2d42b4f574a831f2f1d2222023049d8a

See more details on using hashes here.

File details

Details for the file imagesize-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: imagesize-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.0

File hashes

Hashes for imagesize-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea0c9a0384df69ed86a943a15cde37d0360b82491b3910dc2215e202e62b5b02
MD5 55d8118b99722665927ef2cf405d86ce
BLAKE2b-256 01f9575c8d760eae1fc99651b7cc5efd96ad5379ca4d6b53750b0fb4fe983f34

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

1.5.0

2 files

1.4.1

2 files

1.4.0

3 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

0.7.1

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page