@@ -1653,6 +1653,16 @@ def _save(im, fp, filename):
16531653 except Exception :
16541654 pass # might not be an IFD. Might not have populated type
16551655
1656+ legacy_ifd = {}
1657+ if hasattr (im , "tag" ):
1658+ legacy_ifd = im .tag .to_v2 ()
1659+
1660+ supplied_tags = {** legacy_ifd , ** getattr (im , "tag_v2" , {})}
1661+ if SAMPLEFORMAT in supplied_tags :
1662+ # SAMPLEFORMAT is determined by the image format and should not be copied
1663+ # from legacy_ifd.
1664+ del supplied_tags [SAMPLEFORMAT ]
1665+
16561666 # additions written by Greg Couch, [email protected] 16571667 # inspired by image-sig posting from Kevin Cazabon, [email protected] 16581668 if hasattr (im , "tag_v2" ):
@@ -1666,8 +1676,14 @@ def _save(im, fp, filename):
16661676 XMP ,
16671677 ):
16681678 if key in im .tag_v2 :
1669- ifd [key ] = im .tag_v2 [key ]
1670- ifd .tagtype [key ] = im .tag_v2 .tagtype [key ]
1679+ if key == IPTC_NAA_CHUNK and im .tag_v2 .tagtype [key ] not in (
1680+ TiffTags .BYTE ,
1681+ TiffTags .UNDEFINED ,
1682+ ):
1683+ del supplied_tags [key ]
1684+ else :
1685+ ifd [key ] = im .tag_v2 [key ]
1686+ ifd .tagtype [key ] = im .tag_v2 .tagtype [key ]
16711687
16721688 # preserve ICC profile (should also work when saving other formats
16731689 # which support profiles as TIFF) -- 2008-06-06 Florian Hoech
@@ -1807,16 +1823,6 @@ def _save(im, fp, filename):
18071823 # Merge the ones that we have with (optional) more bits from
18081824 # the original file, e.g x,y resolution so that we can
18091825 # save(load('')) == original file.
1810- legacy_ifd = {}
1811- if hasattr (im , "tag" ):
1812- legacy_ifd = im .tag .to_v2 ()
1813-
1814- # SAMPLEFORMAT is determined by the image format and should not be copied
1815- # from legacy_ifd.
1816- supplied_tags = {** getattr (im , "tag_v2" , {}), ** legacy_ifd }
1817- if SAMPLEFORMAT in supplied_tags :
1818- del supplied_tags [SAMPLEFORMAT ]
1819-
18201826 for tag , value in itertools .chain (ifd .items (), supplied_tags .items ()):
18211827 # Libtiff can only process certain core items without adding
18221828 # them to the custom dictionary.
0 commit comments