Skip to content

Commit c693df6

Browse files
committed
Add tests for
1 parent ba09058 commit c693df6

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

lib/matplotlib/tests/test_image.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def test_nonuniformimage_setnorm():
455455

456456
@knownfailureif(not HAS_PIL)
457457
@cleanup
458-
def test_jpeg_alpha():
458+
def test_flatten():
459459
plt.figure(figsize=(1, 1), dpi=300)
460460
# Create an image that is all black, with a gradient from 0-1 in
461461
# the alpha channel from left to right.
@@ -464,21 +464,43 @@ def test_jpeg_alpha():
464464

465465
plt.figimage(im)
466466

467-
buff = io.BytesIO()
467+
jpg_buf = io.BytesIO()
468+
pngF_buf = io.BytesIO()
469+
png_buf = io.BytesIO()
470+
468471
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)
470476

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)
473484

474485
# If this fails, there will be only one color (all black). If this
475486
# 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+
478495
# 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)
482504

483505

484506
if __name__=='__main__':

0 commit comments

Comments
 (0)