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

Allow uncompressed TIFF images to be saved in chunks #7650

Merged
merged 2 commits into from Jan 1, 2024

Conversation

radarhere
Copy link
Member

Helps #4669

The issue would like to save a large TIFF image, but to do so in chunks, read then loading the entire image into memory at once.

If libtiff isn't used when saving, then the data is saved simply using the "raw" encoder.

So then what if one just appended data to a created image? The header would still need to change so that the image knew about the new size.

from PIL import Image, TiffImagePlugin

im = Image.open("Tests/images/hopper.png")
with open("out.tiff", "wb") as fp:
  for i, chunk in enumerate([
    im.crop((0, 0, 128, 32)),
    im.crop((0, 32, 128, 64)),
    im.crop((0, 64, 128, 96)),
    im.crop((0, 96, 128, 128)),
  ]):
    if i == 0:
      chunk.save(fp, "TIFF", tiffinfo={
        TiffImagePlugin.IMAGEWIDTH: 128,
        TiffImagePlugin.IMAGELENGTH: 128
      })
    else:
      fp.write(chunk.tobytes())

The image is the right size, but black.

When calculating stride, ifd[ROWSPERSTRIP] and ifd[STRIPBYTECOUNTS], TiffImagePlugin currently uses the image size. This PR updates it to use ifd[IMAGEWIDTH] and ifd[IMAGELENGTH], and then the saved image appears correctly.

Apart from this scenario, ifd[IMAGEWIDTH] and ifd[IMAGELENGTH] are supposed to be the same as image width and height. Two of the tests had to be updated when saving TIFF images using existing info, but I could argue that they were already incorrect in main for saving images with an incorrect ifd[IMAGEWIDTH].

@radarhere radarhere added the TIFF label Dec 29, 2023
@radarhere radarhere changed the title Allow TIFF images to be saved in chunks Allow uncompressed TIFF images to be saved in chunks Dec 29, 2023
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a conflict resolving.

@radarhere radarhere merged commit bd7e709 into python-pillow:main Jan 1, 2024
56 checks passed
@radarhere radarhere deleted the tiff branch January 1, 2024 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants