Support writing LONG8 offsets in AppendingTiffWriter by radarhere · Pull Request #8417 · python-pillow/Pillow
| with Image.open("Tests/images/hopper_bigtiff.tif") as im: | |
| # The data type of this file's StripOffsets tag is LONG8, | |
| # which is not yet supported for offset data when saving multiple frames. | |
| del im.tag_v2[273] | |
| outfile = str(tmp_path / "temp.tif") | |
| im.save(outfile, save_all=True, append_images=[im], tiffinfo=im.tag_v2) |
if you try and run the test without removing this tag, an error is raised from AppendingTiffWriter.
def fixOffsets(
self, count: int, isShort: bool = False, isLong: bool = False
) -> None:
if not isShort and not isLong:
msg = "offset is neither short nor long"
> raise RuntimeError(msg)
E RuntimeError: offset is neither short nor long
Extending AppendingTiffWriter to handle LONG8 offsets is made complicated as it explicitly only handles shorts and longs, and has duplicated code to achieve that.
| def rewriteLastShort(self, value: int) -> None: | |
| self.f.seek(-2, os.SEEK_CUR) | |
| bytes_written = self.f.write(struct.pack(self.shortFmt, value)) | |
| self._verify_bytes_written(bytes_written, 2) | |
| def rewriteLastLong(self, value: int) -> None: | |
| self.f.seek(-4, os.SEEK_CUR) | |
| bytes_written = self.f.write(struct.pack(self.longFmt, value)) | |
| self._verify_bytes_written(bytes_written, 4) |
| def writeShort(self, value: int) -> None: | |
| bytes_written = self.f.write(struct.pack(self.shortFmt, value)) | |
| self._verify_bytes_written(bytes_written, 2) | |
| def writeLong(self, value: int) -> None: | |
| bytes_written = self.f.write(struct.pack(self.longFmt, value)) | |
| self._verify_bytes_written(bytes_written, 4) |
| def fixOffsets( | |
| self, count: int, isShort: bool = False, isLong: bool = False | |
| ) -> None: |
Rather than just adding in rewriteLastLong8(), readLong8(), and adding an isLong8 parameter, I've reworked the internals of the class to pass around the field size in my first commit. That made adding support for LONG8 very straightforward in my second commit.