Skip to content

Commit 67d41bf

Browse files
committed
Support writing SIGNED_RATIONAL tag types
1 parent ddd4f00 commit 67d41bf

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
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: 2 additions & 5 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,7 +998,8 @@ 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) {
1001+
} else if (type == TIFF_DOUBLE || type == TIFF_SRATIONAL ||
1002+
type == TIFF_RATIONAL) {
10061003
status = ImagingLibTiffSetField(
10071004
&encoder->state, (ttag_t)key_int, (FLOAT64)PyFloat_AsDouble(value)
10081005
);

0 commit comments

Comments
 (0)