Skip to content

Commit 343a11e

Browse files
committed
Support writing SIGNED_RATIONAL tag types
1 parent ddd4f00 commit 343a11e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

Tests/test_file_libtiff.py

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

358+
def test_signed_rational(
359+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
360+
) -> None:
361+
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
362+
363+
ifd = TiffImagePlugin.ImageFileDirectory_v2()
364+
ifd[37000] = 100
365+
ifd.tagtype[37000] = TiffTags.SIGNED_RATIONAL
366+
367+
out = tmp_path / "temp.tif"
368+
im = Image.new("L", (1, 1))
369+
im.save(out, tiffinfo=ifd)
370+
371+
with Image.open(out) as reloaded:
372+
assert reloaded.tag_v2[37000] == 100
373+
358374
def test_ifd(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
359375
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)
360376

src/encode.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,6 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
990990
status = ImagingLibTiffSetField(
991991
&encoder->state, (ttag_t)key_int, (FLOAT32)PyFloat_AsDouble(value)
992992
);
993-
} else if (type == TIFF_DOUBLE) {
994-
status = ImagingLibTiffSetField(
995-
&encoder->state, (ttag_t)key_int, (FLOAT64)PyFloat_AsDouble(value)
996-
);
997993
} else if (type == TIFF_SBYTE) {
998994
status = ImagingLibTiffSetField(
999995
&encoder->state, (ttag_t)key_int, (INT8)PyLong_AsLong(value)
@@ -1002,11 +998,14 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
1002998
status = ImagingLibTiffSetField(
1003999
&encoder->state, (ttag_t)key_int, PyBytes_AsString(value)
10041000
);
1005-
} else if (type == TIFF_RATIONAL) {
1006-
status = ImagingLibTiffSetField(
1007-
&encoder->state, (ttag_t)key_int, (FLOAT64)PyFloat_AsDouble(value)
1008-
);
1009-
} else if (type == TIFF_LONG8) {
1001+
} else if (type == TIFF_DOUBLE) || type == TIFF_SRATIONAL || type == TIFF_RATIONAL) {
1002+
status = ImagingLibTiffSetField(
1003+
&encoder->state,
1004+
(ttag_t)key_int,
1005+
(FLOAT64)PyFloat_AsDouble(value)
1006+
);
1007+
}
1008+
else if (type == TIFF_LONG8) {
10101009
status = ImagingLibTiffSetField(
10111010
&encoder->state, (ttag_t)key_int, (uint64_t)PyLong_AsLongLong(value)
10121011
);

0 commit comments

Comments
 (0)