Skip to content

Commit ef8d5d3

Browse files
authored
Support saving variable length rational TIFF tags (#9111)
2 parents 6d6f049 + 7afbafd commit ef8d5d3

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Tests/test_file_libtiff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ def test_subifd(self, tmp_path: Path) -> None:
355355
# Should not segfault
356356
im.save(outfile)
357357

358+
def test_whitepoint_tag(
359+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
360+
) -> None:
361+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
362+
363+
out = tmp_path / "temp.tif"
364+
hopper().save(out, tiffinfo={318: (0.3127, 0.3289)})
365+
366+
with Image.open(out) as reloaded:
367+
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
368+
assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289))
369+
358370
def test_xmlpacket_tag(
359371
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
360372
) -> None:

src/encode.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,18 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
922922
);
923923
free(av);
924924
}
925+
} else if (type == TIFF_RATIONAL) {
926+
FLOAT32 *av;
927+
/* malloc check ok, calloc checks for overflow */
928+
av = calloc(len, sizeof(FLOAT32));
929+
if (av) {
930+
for (i = 0; i < len; i++) {
931+
av[i] = (FLOAT32)PyFloat_AsDouble(PyTuple_GetItem(value, i));
932+
}
933+
status =
934+
ImagingLibTiffSetField(&encoder->state, (ttag_t)key_int, av);
935+
free(av);
936+
}
925937
}
926938
} else {
927939
if (type == TIFF_SHORT) {

0 commit comments

Comments
 (0)