BYTE tags of variable length are only single strings

This commit is contained in:
Andrew Murray 2020-05-05 21:20:59 +10:00
parent 794e9f0f0e
commit 7ecb5aaf7e
3 changed files with 17 additions and 23 deletions

View File

@ -319,13 +319,13 @@ def test_empty_values():
def test_PhotoshopInfo(tmp_path):
with Image.open("Tests/images/issue_2278.tif") as im:
assert len(im.tag_v2[34377]) == 1
assert isinstance(im.tag_v2[34377][0], bytes)
assert len(im.tag_v2[34377]) == 70
assert isinstance(im.tag_v2[34377], bytes)
out = str(tmp_path / "temp.tiff")
im.save(out)
with Image.open(out) as reloaded:
assert len(reloaded.tag_v2[34377]) == 1
assert isinstance(reloaded.tag_v2[34377][0], bytes)
assert len(reloaded.tag_v2[34377]) == 70
assert isinstance(reloaded.tag_v2[34377], bytes)
def test_too_many_entries():

View File

@ -573,8 +573,10 @@ class ImageFileDirectory_v2(MutableMapping):
# Spec'd length == 1, Actual > 1, Warn and truncate. Formerly barfed.
# No Spec, Actual length 1, Formerly (<4.2) returned a 1 element tuple.
# Don't mess with the legacy api, since it's frozen.
if (info.length == 1) or (
info.length is None and len(values) == 1 and not legacy_api
if (
(info.length == 1)
or self.tagtype[tag] == TiffTags.BYTE
or (info.length is None and len(values) == 1 and not legacy_api)
):
# Don't mess with the legacy api, since it's frozen.
if legacy_api and self.tagtype[tag] in [

View File

@ -790,28 +790,24 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
if (!is_core_tag) {
// Register field for non core tags.
if (type == TIFF_BYTE) {
is_var_length = 1;
}
if (ImagingLibTiffMergeFieldInfo(&encoder->state, type, key_int, is_var_length)) {
continue;
}
}
if (is_var_length) {
if (type == TIFF_BYTE) {
status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) key_int,
PyBytes_Size(value), PyBytes_AsString(value));
} else if (is_var_length) {
Py_ssize_t len,i;
TRACE(("Setting from Tuple: %d \n", key_int));
len = PyTuple_Size(value);
if (type == TIFF_BYTE) {
UINT8 *av;
/* malloc check ok, calloc checks for overflow */
av = calloc(len, sizeof(UINT8));
if (av) {
for (i=0;i<len;i++) {
av[i] = (UINT8)PyLong_AsLong(PyTuple_GetItem(value,i));
}
status = ImagingLibTiffSetField(&encoder->state, (ttag_t) key_int, len, av);
free(av);
}
} else if (type == TIFF_SHORT) {
if (type == TIFF_SHORT) {
UINT16 *av;
/* malloc check ok, calloc checks for overflow */
av = calloc(len, sizeof(UINT16));
@ -914,10 +910,6 @@ PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args)
status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) key_int,
(FLOAT64)PyFloat_AsDouble(value));
} else if (type == TIFF_BYTE) {
status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) key_int,
(UINT8)PyLong_AsLong(value));
} else if (type == TIFF_SBYTE) {
status = ImagingLibTiffSetField(&encoder->state,
(ttag_t) key_int,