@@ -689,16 +689,21 @@ def _write_multiple_frames(
689689 im_frames [- 1 ].encoderinfo ["duration" ] += encoderinfo ["duration" ]
690690 continue
691691 if im_frames [- 1 ].encoderinfo .get ("disposal" ) == 2 :
692- if background_im is None :
693- color = im .encoderinfo .get (
694- "transparency" , im .info .get ("transparency" , (0 , 0 , 0 ))
695- )
696- background = _get_background (im_frame , color )
697- background_im = Image .new ("P" , im_frame .size , background )
698- first_palette = im_frames [0 ].im .palette
699- assert first_palette is not None
700- background_im .putpalette (first_palette , first_palette .mode )
701- bbox = _getbbox (background_im , im_frame )[1 ]
692+ # To appear correctly in viewers using a convention,
693+ # only consider transparency, and not background color
694+ color = im .encoderinfo .get (
695+ "transparency" , im .info .get ("transparency" )
696+ )
697+ if color is not None :
698+ if background_im is None :
699+ background = _get_background (im_frame , color )
700+ background_im = Image .new ("P" , im_frame .size , background )
701+ first_palette = im_frames [0 ].im .palette
702+ assert first_palette is not None
703+ background_im .putpalette (first_palette , first_palette .mode )
704+ bbox = _getbbox (background_im , im_frame )[1 ]
705+ else :
706+ bbox = (0 , 0 ) + im_frame .size
702707 elif encoderinfo .get ("optimize" ) and im_frame .mode != "1" :
703708 if "transparency" not in encoderinfo :
704709 assert im_frame .palette is not None
@@ -764,7 +769,8 @@ def _write_multiple_frames(
764769 if not palette :
765770 frame_data .encoderinfo ["include_color_table" ] = True
766771
767- im_frame = im_frame .crop (frame_data .bbox )
772+ if frame_data .bbox != (0 , 0 ) + im_frame .size :
773+ im_frame = im_frame .crop (frame_data .bbox )
768774 offset = frame_data .bbox [:2 ]
769775 _write_frame_data (fp , im_frame , offset , frame_data .encoderinfo )
770776 return True
0 commit comments