Python pygame.surfarray() Examples

The following are 30 code examples of pygame.surfarray(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def slow_down_sound(sound, rate):
    """  returns a sound which is a slowed down version of the original.
           rate - at which the sound should be slowed down.  eg. 0.5 would be half speed.
    """

    raise NotImplementedError()
    grow_rate = 1 / rate

    # make it 1/rate times longer.

    a1 = sndarray.array(sound)

    surf = pygame.surfarray.make_surface(a1)
    print (a1.shape[0] * grow_rate)
    scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
    print (scaled_surf)
    print (surf)

    a2 = a1 * rate
    print (a1.shape)
    print (a2.shape)
    print (a2)
    sound2 = sndarray.make_sound(a2.astype(int16))
    return sound2 
Example #2
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def slow_down_sound(sound, rate):
    """  returns a sound which is a slowed down version of the original.
           rate - at which the sound should be slowed down.  eg. 0.5 would be half speed.
    """

    raise NotImplementedError()
    grow_rate = 1 / rate

    # make it 1/rate times longer.

    a1 = sndarray.array(sound)

    surf = pygame.surfarray.make_surface(a1)
    print (a1.shape[0] * grow_rate)
    scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
    print (scaled_surf)
    print (surf)

    a2 = a1 * rate
    print (a1.shape)
    print (a2.shape)
    print (a2)
    sound2 = sndarray.make_sound(a2.astype(int16))
    return sound2 
Example #3
Source File: gen.py    From insightocr with MIT License 6 votes vote down vote up
def addNoiseAndGray(surf):
    # https://stackoverflow.com/questions/34673424/how-to-get-numpy-array-of-rgb-colors-from-pygame-surface
    imgdata = pygame.surfarray.array3d(surf)
    imgdata = imgdata.swapaxes(0, 1)
    # print('imgdata shape %s' % imgdata.shape)  # shall be IMG_HEIGHT * IMG_WIDTH
    imgdata2 = noise_generator('s&p', imgdata)

    img2 = Image.fromarray(np.uint8(imgdata2))
    # img2.save('/home/zhichyu/Downloads/2sp.jpg')
    grayscale2 = ImageOps.grayscale(img2)
    # grayscale2.save('/home/zhichyu/Downloads/2bw2.jpg')
    # return grayscale2

    array = np.asarray(np.uint8(grayscale2))
    # print('array.shape %s' % array.shape)
    selem = disk(random.randint(0, 1))
    eroded = erosion(array, selem)
    return eroded 
Example #4
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixels2d(self):

        sources = [self._make_surface(8),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(32, srcalpha=True)]

        for surf in sources:
            self.assertFalse(surf.get_locked())
            arr = pygame.surfarray.pixels2d(surf)
            self.assertTrue(surf.get_locked())
            self._fill_array2d(arr, surf)
            surf.unlock()
            self.assertTrue(surf.get_locked())
            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())
            self._assert_surface(surf)

        # Error checks
        self.assertRaises(ValueError,
                          pygame.surfarray.pixels2d,
                          self._make_surface(24)) 
Example #5
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_map_array(self):

        arr3d = self._make_src_array3d(uint8)
        targets = [self._make_surface(8),
                   self._make_surface(16),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(24),
                   self._make_surface(32),
                   self._make_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in targets:
            arr2d = pygame.surfarray.map_array(surf, arr3d)
            for posn, i in self.test_points:
                self.assertEqual(arr2d[posn], surf.map_rgb(palette[i]),
                                     "%i != %i, bitsize: %i, flags: %i" %
                                     (arr2d[posn], surf.map_rgb(palette[i]),
                                      surf.get_bitsize(), surf.get_flags()))

        # Exception checks
        self.assertRaises(ValueError, pygame.surfarray.map_array,
                          self._make_surface(32),
                          self._make_array2d(uint8)) 
Example #6
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixels2d(self):

        sources = [self._make_surface(8),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(32, srcalpha=True)]

        for surf in sources:
            self.assertFalse(surf.get_locked())
            arr = pygame.surfarray.pixels2d(surf)
            self.assertTrue(surf.get_locked())
            self._fill_array2d(arr, surf)
            surf.unlock()
            self.assertTrue(surf.get_locked())
            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())
            self._assert_surface(surf)

        # Error checks
        self.assertRaises(ValueError,
                          pygame.surfarray.pixels2d,
                          self._make_surface(24)) 
Example #7
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_array3d(self):

        sources = [self._make_src_surface(16),
                   self._make_src_surface(16, srcalpha=True),
                   self._make_src_surface(24),
                   self._make_src_surface(32),
                   self._make_src_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in sources:
            arr = pygame.surfarray.array3d(surf)
            def same_color(ac, sc):
                return (ac[0] == sc[0] and
                        ac[1] == sc[1] and
                        ac[2] == sc[2])
            for posn, i in self.test_points:
                self.assertTrue(same_color(arr[posn], surf.get_at(posn)),
                                "%s != %s: flags: %i, bpp: %i, posn: %s" % (
                                    tuple(arr[posn]), surf.get_at(posn),
                                    surf.get_flags(), surf.get_bitsize(),
                                    posn)) 
Example #8
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def slow_down_sound(sound, rate):
    """  returns a sound which is a slowed down version of the original.
           rate - at which the sound should be slowed down.  eg. 0.5 would be half speed.
    """

    raise NotImplementedError()
    grow_rate = 1 / rate

    # make it 1/rate times longer.

    a1 = sndarray.array(sound)

    surf = pygame.surfarray.make_surface(a1)
    print (a1.shape[0] * grow_rate)
    scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
    print (scaled_surf)
    print (surf)

    a2 = a1 * rate
    print (a1.shape)
    print (a2.shape)
    print (a2)
    sound2 = sndarray.make_sound(a2.astype(int16))
    return sound2 
Example #9
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixels2d(self):

        sources = [self._make_surface(8),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(32, srcalpha=True)]

        for surf in sources:
            self.assertFalse(surf.get_locked())
            arr = pygame.surfarray.pixels2d(surf)
            self.assertTrue(surf.get_locked())
            self._fill_array2d(arr, surf)
            surf.unlock()
            self.assertTrue(surf.get_locked())
            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())
            self._assert_surface(surf)

        # Error checks
        self.assertRaises(ValueError,
                          pygame.surfarray.pixels2d,
                          self._make_surface(24)) 
Example #10
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_map_array(self):

        arr3d = self._make_src_array3d(uint8)
        targets = [self._make_surface(8),
                   self._make_surface(16),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(24),
                   self._make_surface(32),
                   self._make_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in targets:
            arr2d = pygame.surfarray.map_array(surf, arr3d)
            for posn, i in self.test_points:
                self.assertEqual(arr2d[posn], surf.map_rgb(palette[i]),
                                     "%i != %i, bitsize: %i, flags: %i" %
                                     (arr2d[posn], surf.map_rgb(palette[i]),
                                      surf.get_bitsize(), surf.get_flags()))

        # Exception checks
        self.assertRaises(ValueError, pygame.surfarray.map_array,
                          self._make_surface(32),
                          self._make_array2d(uint8)) 
Example #11
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_array3d(self):

        sources = [self._make_src_surface(16),
                   self._make_src_surface(16, srcalpha=True),
                   self._make_src_surface(24),
                   self._make_src_surface(32),
                   self._make_src_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in sources:
            arr = pygame.surfarray.array3d(surf)
            def same_color(ac, sc):
                return (ac[0] == sc[0] and
                        ac[1] == sc[1] and
                        ac[2] == sc[2])
            for posn, i in self.test_points:
                self.assertTrue(same_color(arr[posn], surf.get_at(posn)),
                                "%s != %s: flags: %i, bpp: %i, posn: %s" % (
                                    tuple(arr[posn]), surf.get_at(posn),
                                    surf.get_flags(), surf.get_bitsize(),
                                    posn)) 
Example #12
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_array3d(self):

        sources = [self._make_src_surface(16),
                   self._make_src_surface(16, srcalpha=True),
                   self._make_src_surface(24),
                   self._make_src_surface(32),
                   self._make_src_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in sources:
            arr = pygame.surfarray.array3d(surf)
            def same_color(ac, sc):
                return (ac[0] == sc[0] and
                        ac[1] == sc[1] and
                        ac[2] == sc[2])
            for posn, i in self.test_points:
                self.assertTrue(same_color(arr[posn], surf.get_at(posn)),
                                "%s != %s: flags: %i, bpp: %i, posn: %s" % (
                                    tuple(arr[posn]), surf.get_at(posn),
                                    surf.get_flags(), surf.get_bitsize(),
                                    posn)) 
Example #13
Source File: sound_array_demos.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def slow_down_sound(sound, rate):
    """  returns a sound which is a slowed down version of the original.
           rate - at which the sound should be slowed down.  eg. 0.5 would be half speed.
    """

    raise NotImplementedError()
    grow_rate = 1 / rate

    # make it 1/rate times longer.

    a1 = sndarray.array(sound)

    surf = pygame.surfarray.make_surface(a1)
    print (a1.shape[0] * grow_rate)
    scaled_surf = pygame.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
    print (scaled_surf)
    print (surf)

    a2 = a1 * rate
    print (a1.shape)
    print (a2.shape)
    print (a2)
    sound2 = sndarray.make_sound(a2.astype(int16))
    return sound2 
Example #14
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_map_array(self):

        arr3d = self._make_src_array3d(uint8)
        targets = [self._make_surface(8),
                   self._make_surface(16),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(24),
                   self._make_surface(32),
                   self._make_surface(32, srcalpha=True)]
        palette = self.test_palette

        for surf in targets:
            arr2d = pygame.surfarray.map_array(surf, arr3d)
            for posn, i in self.test_points:
                self.assertEqual(arr2d[posn], surf.map_rgb(palette[i]),
                                     "%i != %i, bitsize: %i, flags: %i" %
                                     (arr2d[posn], surf.map_rgb(palette[i]),
                                      surf.get_bitsize(), surf.get_flags()))

        # Exception checks
        self.assertRaises(ValueError, pygame.surfarray.map_array,
                          self._make_surface(32),
                          self._make_array2d(uint8)) 
Example #15
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 6 votes vote down vote up
def test_pixels2d(self):

        sources = [self._make_surface(8),
                   self._make_surface(16, srcalpha=True),
                   self._make_surface(32, srcalpha=True)]

        for surf in sources:
            self.assertFalse(surf.get_locked())
            arr = pygame.surfarray.pixels2d(surf)
            self.assertTrue(surf.get_locked())
            self._fill_array2d(arr, surf)
            surf.unlock()
            self.assertTrue(surf.get_locked())
            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())
            self._assert_surface(surf)

        # Error checks
        self.assertRaises(ValueError,
                          pygame.surfarray.pixels2d,
                          self._make_surface(24)) 
Example #16
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_pixels3d(self):

        sources = [self._make_surface(24),
                   self._make_surface(32)]

        for surf in sources:
            self.assertFalse(surf.get_locked())
            arr = pygame.surfarray.pixels3d(surf)
            self.assertTrue(surf.get_locked())
            self._fill_array3d(arr)
            surf.unlock()
            self.assertTrue(surf.get_locked())
            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())
            self._assert_surface(surf)

        # Alpha check
        color = (1, 2, 3, 0)
        surf = self._make_surface(32, srcalpha=True)
        arr = pygame.surfarray.pixels3d(surf)
        arr[0,0] = color[:3]
        self.assertEqual(surf.get_at((0, 0)), color)

        # Error checks
        def do_pixels3d(surf):
            pygame.surfarray.pixels3d(surf)

        self.assertRaises(ValueError,
                          do_pixels3d,
                          self._make_surface(8))
        self.assertRaises(ValueError,
                          do_pixels3d,
                          self._make_surface(16)) 
Example #17
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_surf_lock (self):
        sf = pygame.Surface ((5, 5), 0, 32)
        for atype in pygame.surfarray.get_arraytypes ():
            pygame.surfarray.use_arraytype (atype)

            ar = pygame.surfarray.pixels2d (sf)
            self.assertTrue(sf.get_locked())

            sf.unlock ()
            self.assertTrue(sf.get_locked())

            del ar
            self.assertFalse(sf.get_locked())
            self.assertEqual(sf.get_locks(), ()) 
Example #18
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_array2d(self):

        sources = [self._make_src_surface(8),
                   self._make_src_surface(16),
                   self._make_src_surface(16, srcalpha=True),
                   self._make_src_surface(24),
                   self._make_src_surface(32),
                   self._make_src_surface(32, srcalpha=True)]
        palette = self.test_palette
        alpha_color = (0, 0, 0, 128)

        for surf in sources:
            arr = pygame.surfarray.array2d(surf)
            for posn, i in self.test_points:
                self.assertEqual(arr[posn], surf.get_at_mapped(posn),
                                     "%s != %s: flags: %i, bpp: %i, posn: %s" %
                                     (arr[posn],
                                      surf.get_at_mapped(posn),
                                      surf.get_flags(), surf.get_bitsize(),
                                      posn))

            if surf.get_masks()[3]:
                surf.fill(alpha_color)
                arr = pygame.surfarray.array2d(surf)
                posn = (0, 0)
                self.assertEqual(arr[posn], surf.get_at_mapped(posn),
                                     "%s != %s: bpp: %i" %
                                     (arr[posn],
                                      surf.get_at_mapped(posn),
                                      surf.get_bitsize())) 
Example #19
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_use_arraytype(self):

        def do_use_arraytype(atype):
            pygame.surfarray.use_arraytype(atype)

        pygame.surfarray.use_arraytype('numpy')
        self.assertEqual(pygame.surfarray.get_arraytype(), 'numpy')
        self.assertRaises(ValueError, do_use_arraytype, 'not an option') 
Example #20
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_array_colorkey(self):

        palette = [(0, 0, 0, 0),
                   (10, 50, 100, 255),
                   (60, 120, 240, 130),
                   (64, 128, 255, 0),
                   (255, 128, 0, 65)]
        targets = [self._make_src_surface(8, palette=palette),
                   self._make_src_surface(16, palette=palette),
                   self._make_src_surface(16, palette=palette, srcalpha=True),
                   self._make_src_surface(24, palette=palette),
                   self._make_src_surface(32, palette=palette),
                   self._make_src_surface(32, palette=palette, srcalpha=True)]

        for surf in targets:
            p = palette
            if surf.get_bitsize() == 16:
                p = [surf.unmap_rgb(surf.map_rgb(c)) for c in p]
            surf.set_colorkey(None)
            arr = pygame.surfarray.array_colorkey(surf)
            self.assertTrue(alltrue(arr == 255))

            for i in range(1, len(palette)):
                surf.set_colorkey(p[i])
                alphas = [255] * len(p)
                alphas[i] = 0
                arr = pygame.surfarray.array_colorkey(surf)
                for (x, y), j in self.test_points:
                    self.assertEqual(arr[x, y], alphas[j],
                                         ("%i != %i, posn: (%i, %i), "
                                          "bitsize: %i" %
                                          (arr[x, y], alphas[j],
                                           x, y,
                                           surf.get_bitsize()))) 
Example #21
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_get_arraytype(self):
        array_type = pygame.surfarray.get_arraytype()

        self.assertEqual(array_type, 'numpy',
                         "unknown array type %s" % array_type) 
Example #22
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def _test_pixels_rgb(self, operation, mask_posn):
        method_name = "pixels_" + operation

        pixels_rgb = getattr(pygame.surfarray, method_name)
        palette = [(0, 0, 0, 255),
                   (5, 13, 23, 255),
                   (29, 31, 37, 255),
                   (131, 157, 167, 255),
                   (179, 191, 251, 255)]
        plane = [c[mask_posn] for c in palette]

        surf24 = self._make_src_surface(24, srcalpha=False, palette=palette)
        surf32 = self._make_src_surface(32, srcalpha=False, palette=palette)
        surf32a = self._make_src_surface(32, srcalpha=True, palette=palette)

        for surf in [surf24, surf32, surf32a]:
            self.assertFalse(surf.get_locked())
            arr = pixels_rgb(surf)
            self.assertTrue(surf.get_locked())
            surf.unlock()
            self.assertTrue(surf.get_locked())

            for (x, y), i in self.test_points:
                self.assertEqual(arr[x, y], plane[i])

            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())

        # Check exceptions.
        targets = [(8, False),
                   (16, False),
                   (16, True)]

        for bitsize, srcalpha in targets:
            self.assertRaises(ValueError, pixels_rgb,
                              self._make_surface(bitsize, srcalpha)) 
Example #23
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_get_arraytypes(self):

        arraytypes = pygame.surfarray.get_arraytypes()
        self.assertIn('numpy', arraytypes)

        for atype in arraytypes:
            self.assertEqual(atype, 'numpy', "unknown array type %s" % atype) 
Example #24
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_make_surface(self):

        # How does one properly test this with 2d arrays. It makes no sense
        # since the pixel format is not entirely dependent on element size.
        # Just make sure the surface pixel size is at least as large as the
        # array element size I guess.
        #
        for bitsize, dtype in [(8, uint8), (16, uint16), (24, uint32)]:
## Even this simple assertion fails for 2d arrays. Where's the problem?
##            surf = pygame.surfarray.make_surface(self._make_array2d(dtype))
##            self.assertGreaterEqual(surf.get_bitsize(), bitsize,
##                            "not %i >= %i)" % (surf.get_bitsize(), bitsize))
##
            surf = pygame.surfarray.make_surface(self._make_src_array3d(dtype))
            self._assert_surface(surf)

        # Issue #81: round from float to int
        try:
            rint
        except NameError:
            pass
        else:
            w = 9
            h = 11
            length = w * h
            for dtype in [float32, float64]:
                farr = arange(0, length, dtype=dtype)
                farr.shape = w, h
                surf = pygame.surfarray.make_surface(farr)
                for x in range(w):
                    for y in range(h):
                        self.assertEqual(surf.get_at_mapped((x, y)),
                                         int(rint(farr[x, y]))) 
Example #25
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_get_arraytype(self):
        array_type = pygame.surfarray.get_arraytype()

        self.assertEqual(array_type, 'numpy',
                         "unknown array type %s" % array_type) 
Example #26
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def _test_pixels_rgb(self, operation, mask_posn):
        method_name = "pixels_" + operation

        pixels_rgb = getattr(pygame.surfarray, method_name)
        palette = [(0, 0, 0, 255),
                   (5, 13, 23, 255),
                   (29, 31, 37, 255),
                   (131, 157, 167, 255),
                   (179, 191, 251, 255)]
        plane = [c[mask_posn] for c in palette]

        surf24 = self._make_src_surface(24, srcalpha=False, palette=palette)
        surf32 = self._make_src_surface(32, srcalpha=False, palette=palette)
        surf32a = self._make_src_surface(32, srcalpha=True, palette=palette)

        for surf in [surf24, surf32, surf32a]:
            self.assertFalse(surf.get_locked())
            arr = pixels_rgb(surf)
            self.assertTrue(surf.get_locked())
            surf.unlock()
            self.assertTrue(surf.get_locked())

            for (x, y), i in self.test_points:
                self.assertEqual(arr[x, y], plane[i])

            del arr
            self.assertFalse(surf.get_locked())
            self.assertEqual(surf.get_locks(), ())

        # Check exceptions.
        targets = [(8, False),
                   (16, False),
                   (16, True)]

        for bitsize, srcalpha in targets:
            self.assertRaises(ValueError, pixels_rgb,
                              self._make_surface(bitsize, srcalpha)) 
Example #27
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_use_arraytype(self):

        def do_use_arraytype(atype):
            pygame.surfarray.use_arraytype(atype)

        pygame.surfarray.use_arraytype('numpy')
        self.assertEqual(pygame.surfarray.get_arraytype(), 'numpy')
        self.assertRaises(ValueError, do_use_arraytype, 'not an option') 
Example #28
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_use_arraytype(self):

        def do_use_arraytype(atype):
            pygame.surfarray.use_arraytype(atype)

        pygame.surfarray.use_arraytype('numpy')
        self.assertEqual(pygame.surfarray.get_arraytype(), 'numpy')
        self.assertRaises(ValueError, do_use_arraytype, 'not an option') 
Example #29
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def setUp(cls):
        # This makes sure pygame is always initialized before each test (in
        # case a test calls pygame.quit()).
        if not pygame.get_init():
            pygame.init()

        # Makes sure the same array package is used each time.
        pygame.surfarray.use_arraytype(arraytype) 
Example #30
Source File: surfarray_test.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def test_array2d(self):

        sources = [self._make_src_surface(8),
                   self._make_src_surface(16),
                   self._make_src_surface(16, srcalpha=True),
                   self._make_src_surface(24),
                   self._make_src_surface(32),
                   self._make_src_surface(32, srcalpha=True)]
        palette = self.test_palette
        alpha_color = (0, 0, 0, 128)

        for surf in sources:
            arr = pygame.surfarray.array2d(surf)
            for posn, i in self.test_points:
                self.assertEqual(arr[posn], surf.get_at_mapped(posn),
                                     "%s != %s: flags: %i, bpp: %i, posn: %s" %
                                     (arr[posn],
                                      surf.get_at_mapped(posn),
                                      surf.get_flags(), surf.get_bitsize(),
                                      posn))

            if surf.get_masks()[3]:
                surf.fill(alpha_color)
                arr = pygame.surfarray.array2d(surf)
                posn = (0, 0)
                self.assertEqual(arr[posn], surf.get_at_mapped(posn),
                                     "%s != %s: bpp: %i" %
                                     (arr[posn],
                                      surf.get_at_mapped(posn),
                                      surf.get_bitsize()))