Skip to content

shibukawa/imagesize_py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

145 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

imagesize

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.14

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 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).

Benchmark

It only parses headers, and ignores pixel data. So it is much faster than Pillow.

module result
imagesize (pure Python) 1.077 seconds per 100 000 times
Pillow 10.569 seconds per 100 000 times

I tested on MacBookPro (2014/Core i7) with 125kB PNG files.

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:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages