Ejemplo n.º 1
0
    def testLoadPNG(self):
        """ see if we can load a png with color values in the proper channels.
        """
        # Create a PNG file with known colors
        reddish_pixel = (210, 0, 0, 255)
        greenish_pixel = (0, 220, 0, 255)
        bluish_pixel = (0, 0, 230, 255)
        greyish_pixel = (110, 120, 130, 140)
        pixel_array = [reddish_pixel + greenish_pixel, bluish_pixel + greyish_pixel]

        f_descriptor, f_path = tempfile.mkstemp(suffix=".png")

        with os.fdopen(f_descriptor, "wb") as f:
            w = png.Writer(2, 2, alpha=True)
            w.write(f, pixel_array)

        # Read the PNG file and verify that pygame interprets it correctly
        surf = pygame.image.load(f_path)

        self.assertEqual(surf.get_at((0, 0)), reddish_pixel)
        self.assertEqual(surf.get_at((1, 0)), greenish_pixel)
        self.assertEqual(surf.get_at((0, 1)), bluish_pixel)
        self.assertEqual(surf.get_at((1, 1)), greyish_pixel)

        # Read the PNG file obj. and verify that pygame interprets it correctly
        with open(f_path, "rb") as f:
            surf = pygame.image.load(f)

        self.assertEqual(surf.get_at((0, 0)), reddish_pixel)
        self.assertEqual(surf.get_at((1, 0)), greenish_pixel)
        self.assertEqual(surf.get_at((0, 1)), bluish_pixel)
        self.assertEqual(surf.get_at((1, 1)), greyish_pixel)

        os.remove(f_path)
Ejemplo n.º 2
0
    def testLoadPNG(self):
        """ see if we can load a png with color values in the proper channels.
        """
        # Create a PNG file with known colors
        reddish_pixel = (210, 0, 0, 255)
        greenish_pixel = (0, 220, 0, 255)
        bluish_pixel = (0, 0, 230, 255)
        greyish_pixel = (110, 120, 130, 140)
        pixel_array = [
            reddish_pixel + greenish_pixel, bluish_pixel + greyish_pixel
        ]

        f_descriptor, f_path = tempfile.mkstemp(suffix='.png')
        f = os.fdopen(f_descriptor, 'wb')
        w = png.Writer(2, 2, alpha=True)
        w.write(f, pixel_array)
        f.close()

        # Read the PNG file and verify that pygame interprets it correctly
        surf = pygame.image.load(f_path)

        pixel_x0_y0 = surf.get_at((0, 0))
        pixel_x1_y0 = surf.get_at((1, 0))
        pixel_x0_y1 = surf.get_at((0, 1))
        pixel_x1_y1 = surf.get_at((1, 1))

        self.assertEquals(pixel_x0_y0, reddish_pixel)
        self.assertEquals(pixel_x1_y0, greenish_pixel)
        self.assertEquals(pixel_x0_y1, bluish_pixel)
        self.assertEquals(pixel_x1_y1, greyish_pixel)

        # Read the PNG file obj. and verify that pygame interprets it correctly
        f = open(f_path, 'rb')
        surf = pygame.image.load(f)
        f.close()

        pixel_x0_y0 = surf.get_at((0, 0))
        pixel_x1_y0 = surf.get_at((1, 0))
        pixel_x0_y1 = surf.get_at((0, 1))
        pixel_x1_y1 = surf.get_at((1, 1))

        self.assertEquals(pixel_x0_y0, reddish_pixel)
        self.assertEquals(pixel_x1_y0, greenish_pixel)
        self.assertEquals(pixel_x0_y1, bluish_pixel)
        self.assertEquals(pixel_x1_y1, greyish_pixel)

        os.remove(f_path)
Ejemplo n.º 3
0
    def test_load_basic(self):
        # __doc__ (as of 2008-08-02) for pygame.image.load_basic:

        # pygame.image.load(filename): return Surface
        # pygame.image.load(fileobj, namehint=): return Surface
        # load new image from a file

        # Creating pixels and surface
        cyan_pixel = (0, 255, 255, 255)
        purple_pixel = (255, 0, 255, 255)
        yellow_pixel = (255, 255, 0, 255)
        green_pixel = (0, 255, 0, 255)
        pixel_array = [cyan_pixel + purple_pixel, yellow_pixel + green_pixel]

        surf = pygame.Surface((2, 2))

        surf.set_at((0, 0), cyan_pixel)
        surf.set_at((1, 0), purple_pixel)
        surf.set_at((0, 1), yellow_pixel)
        surf.set_at((1, 1), green_pixel)

        f_descriptor, f_path = tempfile.mkstemp(suffix=".bmp")

        with os.fdopen(f_descriptor, "wb") as f:
            w = png.Writer(2, 2, alpha=True)
            w.write(f, pixel_array)

        # Testing width, height and size of surface
        self.assertEqual(surf.get_size(), pygame.image.load(f_path).get_size())
        self.assertEqual(surf.get_width(),
                         pygame.image.load(f_path).get_width())
        self.assertEqual(surf.get_height(),
                         pygame.image.load(f_path).get_height())

        # Testing color of pixels
        self.assertEqual(surf.get_at((0, 0)),
                         pygame.image.load(f_path).get_at((0, 0)))
        self.assertEqual(surf.get_at((1, 0)),
                         pygame.image.load(f_path).get_at((1, 0)))
        self.assertEqual(surf.get_at((0, 1)),
                         pygame.image.load(f_path).get_at((0, 1)))
        self.assertEqual(surf.get_at((1, 1)),
                         pygame.image.load(f_path).get_at((1, 1)))

        os.remove(f_path)
Ejemplo n.º 4
0
    def test_get_extended(self):
        #Create a png file and try to load it. If it cannot, get_extended() should return False
        raw_image = []
        raw_image.append((200, 200, 200, 255, 100, 100, 100, 255))

        f_descriptor, f_path = tempfile.mkstemp(suffix='.png')

        with os.fdopen(f_descriptor, 'wb') as file:
            w = png.Writer(2, 1, alpha=True)
            w.write(file, raw_image)

        try:
            surf = pygame.image.load(f_path)
            loaded = True
        except pygame.error:
            loaded = False

        self.assertEqual(pygame.image.get_extended(), loaded)
Ejemplo n.º 5
0
    def test_surface_get_at(self):
        cyan_pixel = (0, 255, 255, 255)
        purple_pixel = (255, 0, 255, 255)
        yellow_pixel = (255, 255, 0, 255)
        green_pixel = (0, 255, 0, 255)
        pixel_array = [cyan_pixel + purple_pixel, yellow_pixel + green_pixel]

        surf = pygame.Surface((2, 2))

        surf.set_at((0, 0), cyan_pixel)
        surf.set_at((1, 0), purple_pixel)
        surf.set_at((0, 1), yellow_pixel)
        surf.set_at((1, 1), green_pixel)

        f_descriptor, f_path = tempfile.mkstemp(suffix=".bmp")

        with os.fdopen(f_descriptor, "wb") as f:
            w = png.Writer(2, 2, alpha=True)
            w.write(f, pixel_array)

        self.assertEqual(surf.get_at((0, 0)),
                         pygame.image.load(f_path).get_at((0, 0)))

        os.remove(f_path)