Skip to content

Commit 2d284ae

Browse files
committed
Allow writing of UNDEFINED tags
1 parent 859b275 commit 2d284ae

3 files changed

Lines changed: 30 additions & 20 deletions

File tree

Tests/test_file_libtiff.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,6 @@ def check_tags(tiffinfo):
299299
)
300300
continue
301301

302-
if (
303-
libtiff
304-
and isinstance(value, bytes)
305-
and isinstance(tiffinfo, dict)
306-
):
307-
value = value.decode()
308-
309302
assert reloaded_value == value
310303

311304
# Test with types
@@ -682,6 +675,26 @@ def test_read_icc(self):
682675
TiffImagePlugin.READ_LIBTIFF = False
683676
assert icc == icc_libtiff
684677

678+
def test_write_icc(self, tmp_path):
679+
def check_write(libtiff):
680+
TiffImagePlugin.WRITE_LIBTIFF = libtiff
681+
682+
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
683+
icc_profile = img.info["icc_profile"]
684+
685+
out = str(tmp_path / "temp.tif")
686+
img.save(out, icc_profile=icc_profile)
687+
with Image.open(out) as reloaded:
688+
assert icc_profile == reloaded.info["icc_profile"]
689+
690+
libtiffs = []
691+
if Image.core.libtiff_support_custom_tags:
692+
libtiffs.append(True)
693+
libtiffs.append(False)
694+
695+
for libtiff in libtiffs:
696+
check_write(libtiff)
697+
685698
def test_multipage_compression(self):
686699
with Image.open("Tests/images/compression.tif") as im:
687700

src/PIL/TiffImagePlugin.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,10 @@ def _setitem(self, tag, value, legacy_api):
553553
)
554554
elif all(isinstance(v, float) for v in values):
555555
self.tagtype[tag] = TiffTags.DOUBLE
556-
else:
557-
if all(isinstance(v, str) for v in values):
558-
self.tagtype[tag] = TiffTags.ASCII
556+
elif all(isinstance(v, str) for v in values):
557+
self.tagtype[tag] = TiffTags.ASCII
558+
elif all(isinstance(v, bytes) for v in values):
559+
self.tagtype[tag] = TiffTags.BYTE
559560

560561
if self.tagtype[tag] == TiffTags.UNDEFINED:
561562
values = [
@@ -1548,16 +1549,17 @@ def _save(im, fp, filename):
15481549
# Custom items are supported for int, float, unicode, string and byte
15491550
# values. Other types and tuples require a tagtype.
15501551
if tag not in TiffTags.LIBTIFF_CORE:
1551-
if (
1552-
TiffTags.lookup(tag).type == TiffTags.UNDEFINED
1553-
or not Image.core.libtiff_support_custom_tags
1554-
):
1552+
if not Image.core.libtiff_support_custom_tags:
15551553
continue
15561554

15571555
if tag in ifd.tagtype:
15581556
types[tag] = ifd.tagtype[tag]
15591557
elif not (isinstance(value, (int, float, str, bytes))):
15601558
continue
1559+
else:
1560+
type = TiffTags.lookup(tag).type
1561+
if type:
1562+
types[tag] = type
15611563
if tag not in atts and tag not in blocklist:
15621564
if isinstance(value, str):
15631565
atts[tag] = value.encode("ascii", "replace") + b"\0"

src/encode.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,6 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
761761
}
762762
}
763763

764-
if (PyBytes_Check(value) && type == TIFF_UNDEFINED) {
765-
// For backwards compatibility
766-
type = TIFF_ASCII;
767-
}
768-
769764
if (PyTuple_Check(value)) {
770765
Py_ssize_t len;
771766
len = PyTuple_Size(value);
@@ -797,7 +792,7 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
797792
}
798793
}
799794

800-
if (type == TIFF_BYTE) {
795+
if (type == TIFF_BYTE || type == TIFF_UNDEFINED) {
801796
status = ImagingLibTiffSetField(&encoder->state,
802797
(ttag_t) key_int,
803798
PyBytes_Size(value), PyBytes_AsString(value));

0 commit comments

Comments
 (0)