Debian Codegraph @

README.rst

raw
1 imagesize
2 =============
3
4 .. image:: https://github.com/shibukawa/imagesize_py/actions/workflows/ci.yml/badge.svg
5 :target: https://github.com/shibukawa/imagesize_py/actions/workflows/ci.yml
6
7 .. image:: https://img.shields.io/pypi/v/imagesize.svg
8 :target: https://pypi.org/project/imagesize/
9 :alt: PyPI version
10
11 .. image:: https://img.shields.io/pypi/pyversions/imagesize.svg
12 :target: https://pypi.org/project/imagesize/
13 :alt: Supported Python versions
14
15 .. image:: https://img.shields.io/pypi/l/imagesize.svg
16 :target: https://github.com/shibukawa/imagesize_py/blob/main/LICENSE.rst
17 :alt: License
18
19 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.
20
21 .. code:: python
22
23 import imagesize
24
25 width, height = imagesize.get("test.png")
26 print(width, height)
27
28 xdpi, ydpi = imagesize.getDPI("test.png")
29 print(xdpi, ydpi)
30
31 info = imagesize.get_info("test.png")
32 print(info.width, info.height, info.rotation, info.xdpi, info.ydpi, info.colors, info.channels)
33
34 This module is a pure Python module. You can use file like object like file or something like ``io.BytesIO``.
35
36 Supported Python versions: 3.10-3.15
37
38 Installation
39 ----------------
40
41 .. code:: bash
42
43 pip install imagesize
44
45 For local development setup:
46
47 .. code:: bash
48
49 python -m venv .venv
50 source .venv/bin/activate
51 pip install -e .
52
53 Version 2.0 migration notes
54 -----------------------------
55
56 Version 2.0 includes the following updates:
57
58 * Added/expanded support for ``BMP``.
59 * Added support for ``AVIF``.
60 * Added support for ``HEIC/HEIF``.
61 * ``imagesize.get_info()`` now returns richer metadata including color depth, channel count, and rotation information.
62 * Improved EXIF orientation handling so JPEG, TIFF, AVIF, and HEIC/HEIF return sizes that correctly reflect EXIF rotation metadata.
63 * Added type hints for the public API and related input/output types.
64
65 Backward incompatible behavior in 2.0:
66
67 * ``imagesize.get()`` now returns ``(-1, -1)`` when parsing fails.
68 * ``imagesize.getDPI()`` now returns ``(-1, -1)`` when parsing fails.
69
70 If your existing code relied on exceptions during parse failures, update it to explicitly check return values.
71
72 API
73 -----
74
75 * ``imagesize.get(filepath: FileInput, *, exif_rotation: bool = True) -> tuple[int, int]``
76
77 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.
78 On parsing errors it returns ``(-1, -1)``.
79
80 * ``imagesize.getDPI(filepath: FileInput) -> tuple[int, int]``
81
82 Returns image DPI as ``(xdpi, ydpi)``.
83 On parsing errors it returns ``(-1, -1)``.
84
85 * ``imagesize.get_info(filepath: FileInput, *, size: bool = True, dpi: bool = True, colors: bool = True, exif_rotation: bool = True, channels: bool = True) -> ImageInfo``
86
87 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).
88
89 HTTP and HTTPS URLs are accepted as input. The library uses HTTP byte-range
90 requests automatically so that pixel data does not need to be downloaded. If a
91 server does not support range requests, it transparently falls back to one full
92 download for compatibility. The first request is limited to 8 KiB; later random
93 access expands cached regions in 64 KiB blocks only when more metadata is needed.
94
95 Benchmark
96 ------------
97
98 Only headers and the metadata needed for the requested result are read. Pixel
99 payloads are skipped with ``seek()`` locally and HTTP Range requests remotely.
100 Run the included benchmark with:
101
102 .. code:: bash
103
104 python bench.py --number 10000
105
106 The report includes elapsed time and the number of bytes read for PNG, JPEG,
107 TIFF, GIF, JPEG2000, AVIF, and HEIF. It also simulates an 8 MiB remote AVIF and
108 reports the number of Range requests and transferred bytes.
109
110 Development
111 ---------------
112
113 Run test with the following command:
114
115 .. code:: bash
116
117 python -m unittest
118
119 License
120 -----------
121
122 MIT License
123
124 * test/images/test.heic: https://nokiatech.github.io/heif/examples.html
125 * test/images/test.avif: https://libre-software.net/image/avif-test/
126
127 Thanks
128 ----------
129
130 I referred to the following code:
131
132 * http://markasread.net/post/17551554979/get-image-size-info-using-pure-python-code
133 * https://stackoverflow.com/questions/8032642/how-to-obtain-image-size-using-standard-python-class-without-using-external-lib
134
135 I use sample image from here:
136
137 * https://www.nightprogrammer.org/development/multipage-tiff-example-download-test-image-file/
138
139 Thank you for feedback:
140
141 * tk0miya (https://github.com/tk0miya)
142 * shimizukawa (https://github.com/shimizukawa)
143 * xantares (https://github.com/xantares)
144 * Ivan Zakharyaschev (https://github.com/imz)
145 * Jon Dufresne (https://github.com/jdufresne)
146 * Geoff Lankow (https://github.com/darktrojan)
147 * Hugo (https://github.com/hugovk)
148 * Jack Cherng (https://github.com/jfcherng)
149 * Tyler A. Young (https://github.com/s3cur3)
150 * Mark Browning (https://github.com/mabrowning)
151 * ossdev07 (https://github.com/ossdev07)
152 * Nicholas-Schaub (https://github.com/Nicholas-Schaub)
153 * Nuffknacker (https://github.com/Nuffknacker)
154 * Hannes Römer (https://github.com/hroemer)
155 * mikey (https://github.com/ffreemt)
156 * Marco (https://github.com/marcoffee)
157 * ExtReMLapin (https://github.com/ExtReMLapin)
158 * gremur (https://github.com/gremur)
159 * fuyb1992 (https://github.com/fuyb1992)
160 * flagman (https://github.com/flagman)
161 * icemac (https://github.com/icemac)
162 * ojuuji (https://github.com/ojuuji)
163 * stephenfin (https://github.com/stephenfin)
164 * gaoflow (https://github.com/gaoflow)
165 * jdillard (https://github.com/jdillard)