def testSavePNG24(self): """ see if we can save a png with color values in the proper channels. """ # Create a PNG file with known colors reddish_pixel = (215, 0, 0) greenish_pixel = (0, 225, 0) bluish_pixel = (0, 0, 235) greyish_pixel = (115, 125, 135) surf = pygame.Surface((1, 4), 0, 24) surf.set_at((0, 0), reddish_pixel) surf.set_at((0, 1), greenish_pixel) surf.set_at((0, 2), bluish_pixel) surf.set_at((0, 3), greyish_pixel) f_path = tempfile.mktemp(suffix=".png") pygame.image.save(surf, f_path) try: # Read the PNG file and verify that pygame saved it correctly reader = png.Reader(filename=f_path) width, height, pixels, metadata = reader.asRGB8() # pixels is a generator self.assertEqual(tuple(next(pixels)), reddish_pixel) self.assertEqual(tuple(next(pixels)), greenish_pixel) self.assertEqual(tuple(next(pixels)), bluish_pixel) self.assertEqual(tuple(next(pixels)), greyish_pixel) finally: # Ensures proper clean up. if not reader.file.closed: reader.file.close() del reader os.remove(f_path)
def testSavePNG24(self): """ see if we can save a png with color values in the proper channels. """ # Create a PNG file with known colors reddish_pixel = (215, 0, 0) greenish_pixel = (0, 225, 0) bluish_pixel = (0, 0, 235) greyish_pixel = (115, 125, 135) surf = pygame.Surface((1, 4), 0, 24) surf.set_at((0, 0), reddish_pixel) surf.set_at((0, 1), greenish_pixel) surf.set_at((0, 2), bluish_pixel) surf.set_at((0, 3), greyish_pixel) f_path = tempfile.mktemp(suffix='.png') pygame.image.save(surf, f_path) # Read the PNG file and verify that pygame saved it correctly reader = png.Reader(filename=f_path) width, height, pixels, metadata = reader.asRGB8() pixels_as_tuples = [] for pixel in pixels: pixels_as_tuples.append(tuple(pixel)) self.assertEquals(pixels_as_tuples[0], reddish_pixel) self.assertEquals(pixels_as_tuples[1], greenish_pixel) self.assertEquals(pixels_as_tuples[2], bluish_pixel) self.assertEquals(pixels_as_tuples[3], greyish_pixel) if not reader.file.closed: reader.file.close() del reader os.remove(f_path)
def testSavePaletteAsPNG8(self): """see if we can save a png with color values in the proper channels.""" # Create a PNG file with known colors pygame.display.init() reddish_pixel = (215, 0, 0) greenish_pixel = (0, 225, 0) bluish_pixel = (0, 0, 235) greyish_pixel = (115, 125, 135) surf = pygame.Surface((1, 4), 0, 8) surf.set_palette_at(0, reddish_pixel) surf.set_palette_at(1, greenish_pixel) surf.set_palette_at(2, bluish_pixel) surf.set_palette_at(3, greyish_pixel) f_path = tempfile.mktemp(suffix=".png") pygame.image.save(surf, f_path) try: # Read the PNG file and verify that pygame saved it correctly reader = png.Reader(filename=f_path) reader.read() palette = reader.palette() # pixels is a generator self.assertEqual(tuple(next(palette)), reddish_pixel) self.assertEqual(tuple(next(palette)), greenish_pixel) self.assertEqual(tuple(next(palette)), bluish_pixel) self.assertEqual(tuple(next(palette)), greyish_pixel) finally: # Ensures proper clean up. if not reader.file.closed: reader.file.close() del reader os.remove(f_path)