@@ -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 = [
@@ -573,8 +574,10 @@ def _setitem(self, tag, value, legacy_api):
573574 # Spec'd length == 1, Actual > 1, Warn and truncate. Formerly barfed.
574575 # No Spec, Actual length 1, Formerly (<4.2) returned a 1 element tuple.
575576 # Don't mess with the legacy api, since it's frozen.
576- if (info .length == 1 ) or (
577- info .length is None and len (values ) == 1 and not legacy_api
577+ if (
578+ (info .length == 1 )
579+ or self .tagtype [tag ] == TiffTags .BYTE
580+ or (info .length is None and len (values ) == 1 and not legacy_api )
578581 ):
579582 # Don't mess with the legacy api, since it's frozen.
580583 if legacy_api and self .tagtype [tag ] in [
@@ -1546,16 +1549,17 @@ def _save(im, fp, filename):
15461549 # Custom items are supported for int, float, unicode, string and byte
15471550 # values. Other types and tuples require a tagtype.
15481551 if tag not in TiffTags .LIBTIFF_CORE :
1549- if (
1550- TiffTags .lookup (tag ).type == TiffTags .UNDEFINED
1551- or not Image .core .libtiff_support_custom_tags
1552- ):
1552+ if not Image .core .libtiff_support_custom_tags :
15531553 continue
15541554
15551555 if tag in ifd .tagtype :
15561556 types [tag ] = ifd .tagtype [tag ]
15571557 elif not (isinstance (value , (int , float , str , bytes ))):
15581558 continue
1559+ else :
1560+ type = TiffTags .lookup (tag ).type
1561+ if type :
1562+ types [tag ] = type
15591563 if tag not in atts and tag not in blocklist :
15601564 if isinstance (value , str ):
15611565 atts [tag ] = value .encode ("ascii" , "replace" ) + b"\0 "
0 commit comments