n_frames is incorrect for JPG image
<!--
Thank you for reporting an issue.
Follow these guidelines to ensure your issue is handled properly.
If you have a ...
1. General question: consider asking the question on Stack Overflow
with the python-imaging-library tag:
* https://stackoverflow.com/questions/tagged/python-imaging-library
Do not ask a question in both places.
If you think you have found a bug or have an unexplained exception
then file a bug report here.
2. Bug report: include a self-contained, copy-pastable example that
generates the issue if possible. Be concise with code posted.
Guidelines on how to provide a good bug report:
* https://stackoverflow.com/help/mcve
Bug reports which follow these guidelines are easier to diagnose,
and are often handled much more quickly.
3. Feature request: do a quick search of existing issues
to make sure this has not been asked before.
We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->
### What did you do?
Loading the attached JPG image via `img = PIL.Image.open(...)`
### What did you expect to happen?
`img.n_frames == 1`
### What actually happened?
`img.n_frames == 2`
However, `img.seek(1)` raises an exception. This becomes problematic in downstream tasks like easy.thumbnails' scale-and-crop plugin which applies the command to all frames but fails for the second frame. A similar problem is reported here: https://github.com/SmileyChris/easy-thumbnails/issues/660
I am not sure why or if this JPG has 2 frames. GIMP reports only a single layer.
This may be entirely a problem of the image itself. However, i find it strange that the 2nd frame is reported although it seems to have no data.
### What are your OS, Python and Pillow versions?
* OS: debian/ubuntu (official python docker image)
* Python: 2.7 & 3.13
* Pillow: 6.2.2 & 12.2
(I just included an older version of Pillow to make sure it's not a recent problem)
```text
--------------------------------------------------------------------
Pillow 6.2.2
--------------------------------------------------------------------
Python modules loaded from /usr/local/lib/python2.7/site-packages/PIL
Binary modules loaded from /usr/local/lib/python2.7/site-packages/PIL
--------------------------------------------------------------------
Python 2.7.18 (default, Apr 20 2020, 19:27:10)
[GCC 8.3.0]
--------------------------------------------------------------------
--- PIL CORE support ok
--- TKINTER support ok
--- FREETYPE2 support ok
--- LITTLECMS2 support ok
--- WEBP support ok
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok
--- OPENJPEG (JPEG2000) support ok
--- ZLIB (PNG/ZIP) support ok
--- LIBTIFF support ok
*** RAQM (Bidirectional Text) support not installed
--------------------------------------------------------------------
```
```text
--------------------------------------------------------------------
Pillow 12.2.0
Python 3.14.5 (main, May 11 2026, 23:12:59) [GCC 14.2.0]
--------------------------------------------------------------------
Python executable is /usr/local/bin/python
System Python files loaded from /usr/local
--------------------------------------------------------------------
Python Pillow modules loaded from /usr/local/lib/python3.14/site-packages/PIL
Binary Pillow modules loaded from /usr/local/lib/python3.14/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 12.2.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.14.3
--- LITTLECMS2 support ok, loaded 2.18
--- WEBP support ok, loaded 1.6.0
--- AVIF support ok, loaded 1.4.1
--- JPEG support ok, compiled for libjpeg-turbo 3.1.4.1
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.4
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1, compiled for zlib-ng 2.3.3
--- LIBTIFF support ok, loaded 4.7.1
--- RAQM (Bidirectional Text) support ok, loaded 0.10.3, fribidi 1.0.16, harfbuzz 13.2.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
```
<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.
The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->
#### Dockerimage
```
FROM python:3
WORKDIR /usr/src/app
RUN pip install --no-cache-dir Pillow
COPY . .
CMD [ "python", "./test.py" ]
```
#### test.py
```python
import PIL.Image
img = PIL.Image.open("Borago_officinalis_2015-11-18.JPG")
print("n_frames={}".format(img.n_frames))
for i in range(img.n_frames):
try:
img.seek(i)
print("seek({}): OK".format(i))
except Exception as e:
print("seek({}): {}: {}".format(i, type(e).__name__, e))
```
#### output of Pillow 6.2
```
n_frames=2
seek(0): OK
seek(1): error: unpack_from requires a buffer of at least 2 bytes
```
#### output of Pillow 12.2
```
n_frames=2
seek(0): OK
seek(1): ValueError: No data found for frame
```
<img width="4000" height="3000" alt="Image" src="https://github.com/user-attachments/assets/8d77cc43-1b7a-407a-941c-d7270b45ae1d" />
```shell
jpeginfo -c -i Borago_officinalis_2015-11-18.JPG
Borago_officinalis_2015-11-18.JPG 4000 x 3000 24bit N Exif,XMP,MPF Huffman 1907802 OK
```
Much thanks in advance!!
关闭于 2026-05-17 2 条评论