Skip to content

Errors while processing TIFF images #7845

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

Closed
zhoumeng250 opened this issue Mar 1, 2024 · 5 comments
Closed

Errors while processing TIFF images #7845

zhoumeng250 opened this issue Mar 1, 2024 · 5 comments

Comments

@zhoumeng250
Copy link

zhoumeng250 commented Mar 1, 2024

What did you do?

The code below occasionally encounters an issue where, after resizing the TIF image data and saving it to a new location, the four bytes in the file header of the new TIF file that describe the OFFSET position of the IFD (Image File Directory) are set to zero. This prevents the new resized TIF image from being opened.

What did you expect to happen?

The four bytes describing the OFFSET position of the IFD (Image File Directory) should not be zero.

What actually happened?

The four bytes in the file header of the new TIF file that describe the OFFSET position of the IFD (Image File Directory) are set to zero.

~! Most of the time, there are no this issues. I would like to know the possible reasons that could cause this problem in order to reduce such similar errors.

What are your OS, Python and Pillow versions?

  • OS: Dockerfile - FROM python:3.6

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

  • Python: python:3.6
  • Pillow: Pillow==8.4.0
float_resize_multiple_width = float(os.environ.get('WIDTH_SCALE', 0.3))
float_resize_multiple_height = float(os.environ.get('HEIGHT_SCALE', 0.3))

def process_image(image_path, target_path):
    try:
        with Image.open(image_path) as img:
            width, height = img.size
            new_width = round(width * float_resize_multiple_width)
            new_height = round(height * float_resize_multiple_height)
            resized_img = img.resize((new_width, new_height), Image.LANCZOS)
            resized_img.save(os.path.join(target_path, os.path.basename(image_path)))
            os.remove(image_path)
    except Exception as e:
        print(f"handle {image_path}{e}")
@radarhere
Copy link
Member

radarhere commented Mar 1, 2024

I gather you're not able to upload one of the images that causes the problem? Or even one of the saved images?

I don't suppose you're able to test if the problem is resolved with the latest version of Pillow?

Even if we figure this out, we can't properly fix it, because any fixes we make would be for future versions of Pillow, and you're using an outdated version of Pillow - but I think you know this.

@radarhere radarhere changed the title Encountering errors while processing TIF images with PIL Errors while processing TIFF images Mar 1, 2024
@radarhere radarhere added the TIFF label Mar 1, 2024
@zhoumeng250
Copy link
Author

Yes, I went through the contents of _save(im, fp, filename) in Pillow 8.4.0's TiffImagePlugin.py, but I didn't find anything significant. Also, in my code, the original image is deleted after resizing, so I can't reproduce it. Therefore, I can only speculate that it might be an issue with EMC during program execution or disk bad blocks causing changes in the content of IFH.

@radarhere
Copy link
Member

I'm not seeing anything so far.

If there's no compression, then the offset should be 8.

fp.write(self._prefix + self._pack("HL", 42, 8))

If there's compression, then libtiff would be used. What version of libtiff are you using?

>>> from PIL import features
>>> features.version_codec("libtiff")
'4.6.0'

@radarhere
Copy link
Member

If the problem is in libtiff, then you could avoid it by saving without compression - resized_img.save(..., compression=None)

Copy link

Closing this issue as no feedback has been received.

@github-actions github-actions bot added the Stale label Mar 10, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants