@@ -294,7 +294,7 @@ def _accept(prefix: bytes) -> bool:
294
294
def _limit_rational (
295
295
val : float | Fraction | IFDRational , max_val : int
296
296
) -> tuple [IntegralLike , IntegralLike ]:
297
- inv = abs (float ( val ) ) > 1
297
+ inv = abs (val ) > 1
298
298
n_d = IFDRational (1 / val if inv else val ).limit_rational (max_val )
299
299
return n_d [::- 1 ] if inv else n_d
300
300
@@ -685,22 +685,33 @@ def _setitem(self, tag: int, value: Any, legacy_api: bool) -> None:
685
685
else :
686
686
self .tagtype [tag ] = TiffTags .UNDEFINED
687
687
if all (isinstance (v , IFDRational ) for v in values ):
688
- self .tagtype [tag ] = (
689
- TiffTags .RATIONAL
690
- if all (v >= 0 for v in values )
691
- else TiffTags .SIGNED_RATIONAL
692
- )
688
+ for v in values :
689
+ assert isinstance (v , IFDRational )
690
+ if v < 0 :
691
+ self .tagtype [tag ] = TiffTags .SIGNED_RATIONAL
692
+ break
693
+ else :
694
+ self .tagtype [tag ] = TiffTags .RATIONAL
693
695
elif all (isinstance (v , int ) for v in values ):
694
- if all (0 <= v < 2 ** 16 for v in values ):
696
+ short = True
697
+ signed_short = True
698
+ long = True
699
+ for v in values :
700
+ assert isinstance (v , int )
701
+ if short and not (0 <= v < 2 ** 16 ):
702
+ short = False
703
+ if signed_short and not (- (2 ** 15 ) < v < 2 ** 15 ):
704
+ signed_short = False
705
+ if long and v < 0 :
706
+ long = False
707
+ if short :
695
708
self .tagtype [tag ] = TiffTags .SHORT
696
- elif all ( - ( 2 ** 15 ) < v < 2 ** 15 for v in values ) :
709
+ elif signed_short :
697
710
self .tagtype [tag ] = TiffTags .SIGNED_SHORT
711
+ elif long :
712
+ self .tagtype [tag ] = TiffTags .LONG
698
713
else :
699
- self .tagtype [tag ] = (
700
- TiffTags .LONG
701
- if all (v >= 0 for v in values )
702
- else TiffTags .SIGNED_LONG
703
- )
714
+ self .tagtype [tag ] = TiffTags .SIGNED_LONG
704
715
elif all (isinstance (v , float ) for v in values ):
705
716
self .tagtype [tag ] = TiffTags .DOUBLE
706
717
elif all (isinstance (v , str ) for v in values ):
@@ -718,7 +729,10 @@ def _setitem(self, tag: int, value: Any, legacy_api: bool) -> None:
718
729
719
730
is_ifd = self .tagtype [tag ] == TiffTags .LONG and isinstance (values , dict )
720
731
if not is_ifd :
721
- values = tuple (info .cvt_enum (value ) for value in values )
732
+ values = tuple (
733
+ info .cvt_enum (value ) if isinstance (value , str ) else value
734
+ for value in values
735
+ )
722
736
723
737
dest = self ._tags_v1 if legacy_api else self ._tags_v2
724
738
0 commit comments