@@ -502,8 +502,9 @@ def test_trns_rgb(self) -> None:
502502 im = roundtrip (im )
503503 assert im .info ["transparency" ] == (248 , 248 , 248 )
504504
505- im = roundtrip (im , transparency = (0 , 1 , 2 ))
506- assert im .info ["transparency" ] == (0 , 1 , 2 )
505+ for transparency in ((0 , 1 , 2 ), [0 , 1 , 2 ]):
506+ im = roundtrip (im , transparency = transparency )
507+ assert im .info ["transparency" ] == (0 , 1 , 2 )
507508
508509 def test_trns_p (self , tmp_path : Path ) -> None :
509510 # Check writing a transparency of 0, issue #528
@@ -518,6 +519,36 @@ def test_trns_p(self, tmp_path: Path) -> None:
518519
519520 assert_image_equal (im2 .convert ("RGBA" ), im .convert ("RGBA" ))
520521
522+ def test_trns_invalid (self , tmp_path : Path ) -> None :
523+ out = tmp_path / "temp.png"
524+
525+ for mode in ("1" , "L" , "I;16" ):
526+ im = Image .new (mode , (1 , 1 ))
527+ with pytest .raises (
528+ ValueError , match = f"transparency for { mode } must be an integer"
529+ ):
530+ im .save (out , transparency = "invalid" )
531+
532+ im = Image .new ("I" , (1 , 1 ))
533+ with pytest .warns (DeprecationWarning , match = "Saving I mode images as PNG" ):
534+ with pytest .raises (ValueError ):
535+ im .save (out , transparency = "invalid" )
536+
537+ im = Image .new ("P" , (1 , 1 ))
538+ with pytest .raises (
539+ ValueError , match = "transparency for P must be an integer or bytes"
540+ ):
541+ im .save (out , transparency = "invalid" )
542+
543+ im = Image .new ("RGB" , (1 , 1 ))
544+ with pytest .raises (
545+ ValueError , match = "transparency for RGB must be list or tuple"
546+ ):
547+ im .save (out , transparency = "invalid" )
548+
549+ with pytest .raises (ValueError , match = "transparency for RGB must have length 3" ):
550+ im .save (out , transparency = (1 , 2 ))
551+
521552 def test_trns_null (self ) -> None :
522553 # Check reading images with null tRNS value, issue #1239
523554 test_file = "Tests/images/tRNS_null_1x1.png"
0 commit comments