@@ -455,7 +455,7 @@ def test_nonuniformimage_setnorm():
455
455
456
456
@knownfailureif (not HAS_PIL )
457
457
@cleanup
458
- def test_jpeg_alpha ():
458
+ def test_flatten ():
459
459
plt .figure (figsize = (1 ,1 ),dpi = 300 )
460
460
# Create an image that is all black, with a gradient from 0-1 in
461
461
# the alpha channel from left to right.
@@ -464,21 +464,43 @@ def test_jpeg_alpha():
464
464
465
465
plt .figimage (im )
466
466
467
- buff = io .BytesIO ()
467
+ jpg_buf = io .BytesIO ()
468
+ pngF_buf = io .BytesIO ()
469
+ png_buf = io .BytesIO ()
470
+
468
471
with rc_context ({'savefig.facecolor' :'red' }):
469
- plt .savefig (buff ,transparent = True ,format = 'jpg' ,dpi = 300 )
472
+ plt .savefig (jpg_buf ,transparent = True ,format = 'jpg' ,dpi = 300 )
473
+ plt .savefig (pngF_buf ,transparent = True ,format = 'png' ,
474
+ flatten = True ,dpi = 300 )
475
+ plt .savefig (png_buf ,transparent = True ,format = 'png' ,dpi = 300 )
470
476
471
- buff .seek (0 )
472
- image = Image .open (buff )
477
+ jpg_buf .seek (0 )
478
+ pngF_buf .seek (0 )
479
+ png_buf .seek (0 )
480
+
481
+ jpg_im = Image .open (jpg_buf )
482
+ pngF_im = Image .open (pngF_buf )
483
+ png_im = Image .open (png_buf )
473
484
474
485
# If this fails, there will be only one color (all black). If this
475
486
# is working, we should have all 256 shades of grey represented.
476
- print ("num colors: " ,len (image .getcolors (256 )))
477
- assert len (image .getcolors (256 ))>= 175 and len (image .getcolors (256 ))<= 185
487
+ print ("num colors [jpg]: " ,len (jpg_im .getcolors (256 )))
488
+ print ("num colors [png, flattened]: " ,len (pngF_im .getcolors (256 )))
489
+ print ("num colors [png, not flattened]: " ,len (pngF_im .getcolors (256 )))
490
+
491
+ assert len (jpg_im .getcolors (256 ))>= 175 and len (jpg_im .getcolors (256 ))<= 185
492
+ assert len (pngF_im .getcolor (256 ))== 256
493
+ assert len (png_im .getcolor (256 ))== 256
494
+
478
495
# The fully transparent part should be red, not white or black
479
- # or anything else
480
- print ("corner pixel: " ,image .getpixel ((0 ,0 )))
481
- assert image .getpixel ((0 ,0 ))== (254 ,0 ,0 )
496
+ # or anything else when flattened.
497
+ print ("corner pixel [jpg]: " ,jpg_im .getpixel ((0 ,0 )))
498
+ print ("corner pixel [png, flattened]: " ,pngF_im .getpixel ((0 ,0 )))
499
+ print ("corner pixel [png, not flattened]: " ,png_im .getpixel ((0 ,0 )))
500
+
501
+ assert jpg_im .getpixel ((0 ,0 ))== (254 ,0 ,0 )
502
+ assert pngF_im .getpixel ((0 ,0 ))== (255 ,0 ,0 ,255 )
503
+ assert png_im .getpixel ((0 ,0 ))== (255 ,255 ,255 ,0 )
482
504
483
505
484
506
if __name__ == '__main__' :