mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-29 10:13:09 +03:00
Allow writing of UNDEFINED tags
This commit is contained in:
parent
859b27572b
commit
2d284aea12
|
@ -299,13 +299,6 @@ class TestFileLibTiff(LibTiffTestCase):
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if (
|
|
||||||
libtiff
|
|
||||||
and isinstance(value, bytes)
|
|
||||||
and isinstance(tiffinfo, dict)
|
|
||||||
):
|
|
||||||
value = value.decode()
|
|
||||||
|
|
||||||
assert reloaded_value == value
|
assert reloaded_value == value
|
||||||
|
|
||||||
# Test with types
|
# Test with types
|
||||||
|
@ -682,6 +675,26 @@ class TestFileLibTiff(LibTiffTestCase):
|
||||||
TiffImagePlugin.READ_LIBTIFF = False
|
TiffImagePlugin.READ_LIBTIFF = False
|
||||||
assert icc == icc_libtiff
|
assert icc == icc_libtiff
|
||||||
|
|
||||||
|
def test_write_icc(self, tmp_path):
|
||||||
|
def check_write(libtiff):
|
||||||
|
TiffImagePlugin.WRITE_LIBTIFF = libtiff
|
||||||
|
|
||||||
|
with Image.open("Tests/images/hopper.iccprofile.tif") as img:
|
||||||
|
icc_profile = img.info["icc_profile"]
|
||||||
|
|
||||||
|
out = str(tmp_path / "temp.tif")
|
||||||
|
img.save(out, icc_profile=icc_profile)
|
||||||
|
with Image.open(out) as reloaded:
|
||||||
|
assert icc_profile == reloaded.info["icc_profile"]
|
||||||
|
|
||||||
|
libtiffs = []
|
||||||
|
if Image.core.libtiff_support_custom_tags:
|
||||||
|
libtiffs.append(True)
|
||||||
|
libtiffs.append(False)
|
||||||
|
|
||||||
|
for libtiff in libtiffs:
|
||||||
|
check_write(libtiff)
|
||||||
|
|
||||||
def test_multipage_compression(self):
|
def test_multipage_compression(self):
|
||||||
with Image.open("Tests/images/compression.tif") as im:
|
with Image.open("Tests/images/compression.tif") as im:
|
||||||
|
|
||||||
|
|
|
@ -553,9 +553,10 @@ class ImageFileDirectory_v2(MutableMapping):
|
||||||
)
|
)
|
||||||
elif all(isinstance(v, float) for v in values):
|
elif all(isinstance(v, float) for v in values):
|
||||||
self.tagtype[tag] = TiffTags.DOUBLE
|
self.tagtype[tag] = TiffTags.DOUBLE
|
||||||
else:
|
elif all(isinstance(v, str) for v in values):
|
||||||
if all(isinstance(v, str) for v in values):
|
self.tagtype[tag] = TiffTags.ASCII
|
||||||
self.tagtype[tag] = TiffTags.ASCII
|
elif all(isinstance(v, bytes) for v in values):
|
||||||
|
self.tagtype[tag] = TiffTags.BYTE
|
||||||
|
|
||||||
if self.tagtype[tag] == TiffTags.UNDEFINED:
|
if self.tagtype[tag] == TiffTags.UNDEFINED:
|
||||||
values = [
|
values = [
|
||||||
|
@ -1548,16 +1549,17 @@ def _save(im, fp, filename):
|
||||||
# Custom items are supported for int, float, unicode, string and byte
|
# Custom items are supported for int, float, unicode, string and byte
|
||||||
# values. Other types and tuples require a tagtype.
|
# values. Other types and tuples require a tagtype.
|
||||||
if tag not in TiffTags.LIBTIFF_CORE:
|
if tag not in TiffTags.LIBTIFF_CORE:
|
||||||
if (
|
if not Image.core.libtiff_support_custom_tags:
|
||||||
TiffTags.lookup(tag).type == TiffTags.UNDEFINED
|
|
||||||
or not Image.core.libtiff_support_custom_tags
|
|
||||||
):
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if tag in ifd.tagtype:
|
if tag in ifd.tagtype:
|
||||||
types[tag] = ifd.tagtype[tag]
|
types[tag] = ifd.tagtype[tag]
|
||||||
elif not (isinstance(value, (int, float, str, bytes))):
|
elif not (isinstance(value, (int, float, str, bytes))):
|
||||||
continue
|
continue
|
||||||
|
else:
|
||||||
|
type = TiffTags.lookup(tag).type
|
||||||
|
if type:
|
||||||
|
types[tag] = type
|
||||||
if tag not in atts and tag not in blocklist:
|
if tag not in atts and tag not in blocklist:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
atts[tag] = value.encode("ascii", "replace") + b"\0"
|
atts[tag] = value.encode("ascii", "replace") + b"\0"
|
||||||
|
|
|
@ -761,11 +761,6 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyBytes_Check(value) && type == TIFF_UNDEFINED) {
|
|
||||||
// For backwards compatibility
|
|
||||||
type = TIFF_ASCII;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PyTuple_Check(value)) {
|
if (PyTuple_Check(value)) {
|
||||||
Py_ssize_t len;
|
Py_ssize_t len;
|
||||||
len = PyTuple_Size(value);
|
len = PyTuple_Size(value);
|
||||||
|
@ -797,7 +792,7 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == TIFF_BYTE) {
|
if (type == TIFF_BYTE || type == TIFF_UNDEFINED) {
|
||||||
status = ImagingLibTiffSetField(&encoder->state,
|
status = ImagingLibTiffSetField(&encoder->state,
|
||||||
(ttag_t) key_int,
|
(ttag_t) key_int,
|
||||||
PyBytes_Size(value), PyBytes_AsString(value));
|
PyBytes_Size(value), PyBytes_AsString(value));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user