@@ -1658,6 +1658,16 @@ def _save(im, fp, filename):
16581658 except Exception :
16591659 pass # might not be an IFD. Might not have populated type
16601660
1661+ legacy_ifd = {}
1662+ if hasattr (im , "tag" ):
1663+ legacy_ifd = im .tag .to_v2 ()
1664+
1665+ supplied_tags = {** legacy_ifd , ** getattr (im , "tag_v2" , {})}
1666+ if SAMPLEFORMAT in supplied_tags :
1667+ # SAMPLEFORMAT is determined by the image format and should not be copied
1668+ # from legacy_ifd.
1669+ del supplied_tags [SAMPLEFORMAT ]
1670+
16611671 # additions written by Greg Couch, [email protected] 16621672 # inspired by image-sig posting from Kevin Cazabon, [email protected] 16631673 if hasattr (im , "tag_v2" ):
@@ -1671,8 +1681,14 @@ def _save(im, fp, filename):
16711681 XMP ,
16721682 ):
16731683 if key in im .tag_v2 :
1674- ifd [key ] = im .tag_v2 [key ]
1675- ifd .tagtype [key ] = im .tag_v2 .tagtype [key ]
1684+ if key == IPTC_NAA_CHUNK and im .tag_v2 .tagtype [key ] not in (
1685+ TiffTags .BYTE ,
1686+ TiffTags .UNDEFINED ,
1687+ ):
1688+ del supplied_tags [key ]
1689+ else :
1690+ ifd [key ] = im .tag_v2 [key ]
1691+ ifd .tagtype [key ] = im .tag_v2 .tagtype [key ]
16761692
16771693 # preserve ICC profile (should also work when saving other formats
16781694 # which support profiles as TIFF) -- 2008-06-06 Florian Hoech
@@ -1812,16 +1828,6 @@ def _save(im, fp, filename):
18121828 # Merge the ones that we have with (optional) more bits from
18131829 # the original file, e.g x,y resolution so that we can
18141830 # save(load('')) == original file.
1815- legacy_ifd = {}
1816- if hasattr (im , "tag" ):
1817- legacy_ifd = im .tag .to_v2 ()
1818-
1819- # SAMPLEFORMAT is determined by the image format and should not be copied
1820- # from legacy_ifd.
1821- supplied_tags = {** getattr (im , "tag_v2" , {}), ** legacy_ifd }
1822- if SAMPLEFORMAT in supplied_tags :
1823- del supplied_tags [SAMPLEFORMAT ]
1824-
18251831 for tag , value in itertools .chain (ifd .items (), supplied_tags .items ()):
18261832 # Libtiff can only process certain core items without adding
18271833 # them to the custom dictionary.
0 commit comments