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

not loading big images | switch in PIL 'ImageFile.py' #246

Open
abcnorio opened this issue Jan 22, 2025 · 2 comments
Open

not loading big images | switch in PIL 'ImageFile.py' #246

abcnorio opened this issue Jan 22, 2025 · 2 comments

Comments

@abcnorio
Copy link

Hello,

the following error:

Traceback (most recent call last):
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/ImageFile.py", line 280, in load
    s = read(self.decodermaxblock)
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/PngImagePlugin.py", line 989, in load_read
    cid, pos, length = self.png.read()
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/PngImagePlugin.py", line 173, in read
    length = i32(s)
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/_binary.py", line 95, in i32be
    return unpack_from(">I", c, o)[0]
struct.error: unpack_from requires a buffer of at least 4 bytes for unpacking 4 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 "/home/pala/iqa-pytorch/IQA-PyTorch/inference_iqa.py", line 98, in <module>
    main()
  File "/home/pala/iqa-pytorch/IQA-PyTorch/inference_iqa.py", line 68, in main
    score = iqa_model(img_path, ref_img_path).cpu().item()
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/pala/iqa-pytorch/IQA-PyTorch/pyiqa/models/inference_model.py", line 92, in forward
    target = imread2tensor(target, rgb=True)
  File "/home/pala/iqa-pytorch/IQA-PyTorch/pyiqa/utils/img_util.py", line 72, in imread2tensor
    img = img.convert('RGB')
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/Image.py", line 993, in convert
    self.load()
  File "/home/pala/miniconda3/envs/iqa/lib/python3.10/site-packages/PIL/ImageFile.py", line 287, in load
    raise OSError(msg) from e
OSError: image file is truncated

seems to occur with bigger images for which iqa-pytorch has no problem to calc a score (e.g. hyperiqa). The fix is:

  • go to conda env and do find . -type f -iname 'ImageFile.py'
  • result e.g. - file must be from 'PIL' ./lib/python3.10/site-packages/PIL/ImageFile.py
  • edit that file and change

LOAD_TRUNCATED_IMAGES = False

to

LOAD_TRUNCATED_IMAGES = True

Then it works. I tried to add in file pyiqa/utils/img_util.py

from PIL import Image
Image.LOAD_TRUNCATED_IMAGES = True

to your script but this did not work so I used the global option. Maybe you can add a switch to your script(s) for that.

@chaofengc
Copy link
Owner

We do use this configuration when necessary, as shown in the following example:

ImageFile.LOAD_TRUNCATED_IMAGES = True

However, setting it as default could cause issues like increased memory usage, compatibility problems, or user confusion. It’s likely better to leave it for users to enable explicitly when needed.

@abcnorio
Copy link
Author

ok thx, yes that's uinderstandable.

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

No branches or pull requests

2 participants