@@ -327,14 +327,13 @@ def test_loading_multiple_palettes(path: str, mode: str) -> None:
327327
328328 im .seek (1 )
329329 assert im .mode == mode
330- if mode == "RGBA" :
331- im = im .convert ("RGB" )
330+ im_rgb = im .convert ("RGB" ) if mode == "RGBA" else im
332331
333332 # Check a color only from the old palette
334- assert im .getpixel ((0 , 0 )) == original_color
333+ assert im_rgb .getpixel ((0 , 0 )) == original_color
335334
336335 # Check a color from the new palette
337- assert im .getpixel ((24 , 24 )) not in first_frame_colors
336+ assert im_rgb .getpixel ((24 , 24 )) not in first_frame_colors
338337
339338
340339def test_headers_saving_for_animated_gifs (tmp_path : Path ) -> None :
@@ -354,16 +353,16 @@ def test_palette_handling(tmp_path: Path) -> None:
354353 # see https://github.com/python-pillow/Pillow/issues/513
355354
356355 with Image .open (TEST_GIF ) as im :
357- im = im .convert ("RGB" )
356+ im_rgb = im .convert ("RGB" )
358357
359- im = im .resize ((100 , 100 ), Image .Resampling .LANCZOS )
360- im2 = im .convert ("P" , palette = Image .Palette .ADAPTIVE , colors = 256 )
358+ im_rgb = im_rgb .resize ((100 , 100 ), Image .Resampling .LANCZOS )
359+ im_p = im_rgb .convert ("P" , palette = Image .Palette .ADAPTIVE , colors = 256 )
361360
362- f = tmp_path / "temp.gif"
363- im2 .save (f , optimize = True )
361+ f = tmp_path / "temp.gif"
362+ im_p .save (f , optimize = True )
364363
365364 with Image .open (f ) as reloaded :
366- assert_image_similar (im , reloaded .convert ("RGB" ), 10 )
365+ assert_image_similar (im_rgb , reloaded .convert ("RGB" ), 10 )
367366
368367
369368def test_palette_434 (tmp_path : Path ) -> None :
@@ -383,35 +382,36 @@ def roundtrip(im: Image.Image, **kwargs: bool) -> Image.Image:
383382 with roundtrip (im , optimize = True ) as reloaded :
384383 assert_image_similar (im , reloaded , 1 )
385384
386- im = im .convert ("RGB" )
387- # check automatic P conversion
388- with roundtrip (im ) as reloaded :
389- reloaded = reloaded .convert ("RGB" )
390- assert_image_equal (im , reloaded )
385+ im_rgb = im .convert ("RGB" )
386+
387+ # check automatic P conversion
388+ with roundtrip (im_rgb ) as reloaded :
389+ reloaded = reloaded .convert ("RGB" )
390+ assert_image_equal (im_rgb , reloaded )
391391
392392
393393@pytest .mark .skipif (not netpbm_available (), reason = "Netpbm not available" )
394394def test_save_netpbm_bmp_mode (tmp_path : Path ) -> None :
395395 with Image .open (TEST_GIF ) as img :
396- img = img .convert ("RGB" )
396+ img_rgb = img .convert ("RGB" )
397397
398- tempfile = str (tmp_path / "temp.gif" )
399- b = BytesIO ()
400- GifImagePlugin ._save_netpbm (img , b , tempfile )
401- with Image .open (tempfile ) as reloaded :
402- assert_image_similar (img , reloaded .convert ("RGB" ), 0 )
398+ tempfile = str (tmp_path / "temp.gif" )
399+ b = BytesIO ()
400+ GifImagePlugin ._save_netpbm (img_rgb , b , tempfile )
401+ with Image .open (tempfile ) as reloaded :
402+ assert_image_similar (img_rgb , reloaded .convert ("RGB" ), 0 )
403403
404404
405405@pytest .mark .skipif (not netpbm_available (), reason = "Netpbm not available" )
406406def test_save_netpbm_l_mode (tmp_path : Path ) -> None :
407407 with Image .open (TEST_GIF ) as img :
408- img = img .convert ("L" )
408+ img_l = img .convert ("L" )
409409
410410 tempfile = str (tmp_path / "temp.gif" )
411411 b = BytesIO ()
412- GifImagePlugin ._save_netpbm (img , b , tempfile )
412+ GifImagePlugin ._save_netpbm (img_l , b , tempfile )
413413 with Image .open (tempfile ) as reloaded :
414- assert_image_similar (img , reloaded .convert ("L" ), 0 )
414+ assert_image_similar (img_l , reloaded .convert ("L" ), 0 )
415415
416416
417417def test_seek () -> None :
@@ -1038,26 +1038,26 @@ def test_webp_background(tmp_path: Path) -> None:
10381038 im .save (out )
10391039
10401040 # Test non-opaque WebP background
1041- im = Image .new ("L" , (100 , 100 ), "#000" )
1042- im .info ["background" ] = (0 , 0 , 0 , 0 )
1043- im .save (out )
1041+ im2 = Image .new ("L" , (100 , 100 ), "#000" )
1042+ im2 .info ["background" ] = (0 , 0 , 0 , 0 )
1043+ im2 .save (out )
10441044
10451045
10461046def test_comment (tmp_path : Path ) -> None :
10471047 with Image .open (TEST_GIF ) as im :
10481048 assert im .info ["comment" ] == b"File written by Adobe Photoshop\xa8 4.0"
10491049
10501050 out = tmp_path / "temp.gif"
1051- im = Image .new ("L" , (100 , 100 ), "#000" )
1052- im .info ["comment" ] = b"Test comment text"
1053- im .save (out )
1051+ im2 = Image .new ("L" , (100 , 100 ), "#000" )
1052+ im2 .info ["comment" ] = b"Test comment text"
1053+ im2 .save (out )
10541054 with Image .open (out ) as reread :
1055- assert reread .info ["comment" ] == im .info ["comment" ]
1055+ assert reread .info ["comment" ] == im2 .info ["comment" ]
10561056
1057- im .info ["comment" ] = "Test comment text"
1058- im .save (out )
1057+ im2 .info ["comment" ] = "Test comment text"
1058+ im2 .save (out )
10591059 with Image .open (out ) as reread :
1060- assert reread .info ["comment" ] == im .info ["comment" ].encode ()
1060+ assert reread .info ["comment" ] == im2 .info ["comment" ].encode ()
10611061
10621062 # Test that GIF89a is used for comments
10631063 assert reread .info ["version" ] == b"GIF89a"
0 commit comments