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

Photoshop PSD errors with Truncated File Read #7482

Closed
kporangehat opened this issue Oct 19, 2023 · 4 comments · Fixed by #7483
Closed

Photoshop PSD errors with Truncated File Read #7482

kporangehat opened this issue Oct 19, 2023 · 4 comments · Fixed by #7483

Comments

@kporangehat
Copy link

What did you do?

Tried to open a Photoshop psd file that loads fine in Photoshop.

This issue came up as I'm processing multiple Photoshop psd files at a time on a project. Some files work fine, others do not. All of the files open fine in Photoshop 2023.

Ultimately for this purpose I'm just looking to get the dimensions of the file and confirm the format. But in the future would like to actually be doing more processing on these files.

What did you expect to happen?

I expected the file to be opened.

What actually happened?

The code errors with OSError: Truncated File Read despite using the ImageFile.LOAD_TRUNCATED_IMAGES = True option.

This same result happens whether opening from a file path or loading bytes and opening from there. I tried several methods recommended in previous Github issue submissions.

What are your OS, Python and Pillow versions?

  • OS: MacOS Sonoma 14.0 (23A344)
  • Python: 3.9.17
  • Pillow: 10.1.0
python
Python 3.9.17 (main, Aug 18 2023, 13:18:41)
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import ImageFile, Image
>>> ImageFile.LOAD_TRUNCATED_IMAGES = True
>>> psd_file = "/Users/kp/dev/pil_psd_test/file.psd"
>>> psd = Image.open(fp=psd_file)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3289, in open
    im = _open_core(
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3270, in _open_core
    im = factory(fp, filename)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 117, in __init__
    self._open()
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 127, in _open
    self.layers = _layerinfo(_layer_data, size)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 224, in _layerinfo
    length = i32(read(4))
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 168, in read
    return ImageFile._safe_read(fp, size)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 571, in _safe_read
    raise OSError(msg)
OSError: Truncated File Read

>>> from io import BytesIO
>>> with open(psd_file, 'rb') as fp:
...     image_data = fp.read()
...     bio = BytesIO(image_data)
...     with Image.open(bio) as fo:
...         try:
...             fo.load()
...         except OSError as e:
...             pass
...         img = fo.convert('RGB')
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3284, in open
    im = _open_core(fp, filename, prefix, formats)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/Image.py", line 3270, in _open_core
    im = factory(fp, filename)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 117, in __init__
    self._open()
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 127, in _open
    self.layers = _layerinfo(_layer_data, size)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 224, in _layerinfo
    length = i32(read(4))
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/PsdImagePlugin.py", line 168, in read
    return ImageFile._safe_read(fp, size)
  File "/Users/kp/dev/pil_psd_test/.venv/lib/python3.9/site-packages/PIL/ImageFile.py", line 571, in _safe_read
    raise OSError(msg)
OSError: Truncated File Read
@radarhere
Copy link
Member

Could you attach a copy of a problem file?

@kporangehat
Copy link
Author

Attached
file.psd.zip

@radarhere
Copy link
Member

radarhere commented Oct 20, 2023

Thanks.

Taking a look, I see that as Pillow is processing the layers, it detects that one of them has more than 4 channels. This would be the layer that is RGBA and has a layer mask. Pillow attempts to move on to the next layer, but forgets to skip past the rest of the layer data.

I've created PR #7483 to resolve this, by correctly skipping past the rest of the layer data.

With that change, the following simple code can load and resave your file as the following PNG.

from PIL import Image
psd = Image.open("file.psd")
psd.save("out.png")

@kporangehat
Copy link
Author

Wow, thank you. Incredible response time and very informative. Appreciate your hard work and help! 🙏

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