Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,21 @@ def test_tag_type(
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
assert reloaded.tag_v2[37000] == 100

@pytest.mark.parametrize("tagtype", (TiffTags.BYTE, TiffTags.ASCII))
def test_non_bytes(
self, tagtype: int, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", True)

ifd = TiffImagePlugin.ImageFileDirectory_v2()
ifd[37000] = 100
ifd.tagtype[37000] = tagtype

out = tmp_path / "temp.tif"
im = Image.new("L", (1, 1))
with pytest.raises(ValueError, match="Incorrect tag value type"):
im.save(out, tiffinfo=ifd)

def test_inknames_tag(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
Expand Down
10 changes: 10 additions & 0 deletions src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {

if (type == TIFF_BYTE || type == TIFF_UNDEFINED ||
key_int == TIFFTAG_INKNAMES) {
if (!PyBytes_Check(value)) {
Py_DECREF(encoder);
PyErr_SetString(PyExc_ValueError, "Incorrect tag value type");
return NULL;
}
status = ImagingLibTiffSetField(
&encoder->state,
(ttag_t)key_int,
Expand Down Expand Up @@ -1022,6 +1027,11 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) {
&encoder->state, (ttag_t)key_int, (INT8)PyLong_AsLong(value)
);
} else if (type == TIFF_ASCII) {
if (!PyBytes_Check(value)) {
Py_DECREF(encoder);
PyErr_SetString(PyExc_ValueError, "Incorrect tag value type");
return NULL;
}
status = ImagingLibTiffSetField(
&encoder->state, (ttag_t)key_int, PyBytes_AsString(value)
);
Expand Down
Loading