Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image.open() fails with struct.error with Photoshop .psd files #7704

Closed
kporangehat opened this issue Jan 9, 2024 · 1 comment · Fixed by #7706
Closed

Image.open() fails with struct.error with Photoshop .psd files #7704

kporangehat opened this issue Jan 9, 2024 · 1 comment · Fixed by #7706

Comments

@kporangehat
Copy link

What did you do?

Trying to process a bunch of Photoshop images primarily to obtain their size, mode, and resolution. These files open fine natively without warnings or errors.

Sample file process:

from PIL import ImageFile, Image

ImageFile.LOAD_TRUNCATED_IMAGES = True

with Image.open("/Users/kp/dev/pil_psd_test/struct_error_file.psd") as img:
    print(img.format, img.size, img.mode)

What did you expect to happen?

The file to open and report the format, size, and mode

What actually happened?

Traceback (most recent call last):
  File "/Users/kp/dev/sandbox/pil_psd_test.py", line 48, in <module>
    with Image.open("/Users/kp/dev/pil_psd_test/struct_error_file.psd") as img:
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3309, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file '/Users/kp/dev/pil_psd_test/struct_error_file.psd'

Turning on the debug log message here, I can see the actual error is:

...
DEBUG:PIL.Image:
Traceback (most recent call last):
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 137, in __init__
    self._open()
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 132, in _open
    self.layers = _layerinfo(_layer_data, size)
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 249, in _layerinfo
    t = _maketile(fp, m, bbox, 1)
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 261, in _maketile
    compression = i16(read(2))
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/_binary.py", line 81, in i16be
    return unpack_from(">H", c, o)[0]
struct.error: unpack_from requires a buffer of at least 2 bytes for unpacking 2 bytes at offset 0 (actual buffer size is 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3276, in _open_core
    im = factory(fp, filename)
  File "/Users/kp/dev/sandbox/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 145, in __init__
    raise SyntaxError(v) from v
SyntaxError: unpack_from requires a buffer of at least 2 bytes for unpacking 2 bytes at offset 0 (actual buffer size is 0)
...

Sample image is here:
struct_error_file.psd.zip

What are your OS, Python and Pillow versions?

  • OS: MacOS 14.2.1 (23C71)
  • Python: 3.9.17
  • Pillow: 10.2.0
see above
@radarhere
Copy link
Member

Looking at your image with GIMP, one of the layers has a negative offset.
bg1

I've created PR #7706 to handle this. With that,

from PIL import Image
with Image.open("struct_error_file.psd") as img:
    img.save("out.png")

gives
out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants