Python pygame 模块,PixelArray() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygame.PixelArray()

项目:sc8pr    作者:dmaccarthy    | 项目源码 | 文件源码
def apply(self, img, n=0):
        "Modify image based on equation provided"
        srf, size = self.srfSize(img, True)
        h = size[1]
        pxa = PixelArray(srf)
        x = 0
        for pxCol in pxa:
            y = self.eqn(x, n, size)
            if type(y) is tuple: y, above = y
            else: above = True
            if type(y) is float: y = int(y)
            if above:
                if y < h - 1:
                    if y < 0: y = 0
                    pxCol[y:] = self.fill
            elif y > 0:
                if y > h: y = h
                pxCol[:y] = self.fill
            x += 1
        return srf
项目:pytari2600    作者:ajgrah2000    | 项目源码 | 文件源码
def display_test():
    pygame.init()
    screen = pygame.display.set_mode((800,400))
    pygame.display.set_caption('Pygame')
    pygame.mouse.set_visible(0)

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250,250,250))

    pxarray = pygame.PixelArray(background)

    pxarray[:] = (i%250,i%25,i%150)

    del pxarray

    screen.blit(background, (0,0))
    pygame.display.flip()
    time.sleep(3)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_pixel_array (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            self.assertEqual (ar._pixels_address, sf._pixels_address)

            if sf.mustlock ():
                self.assertTrue (sf.get_locked ())

            self.assertEqual (len (ar), 10)

            del ar
            if sf.mustlock ():
                self.assertFalse (sf.get_locked ())
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_get_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 255))
            val = sf.map_rgb ((0, 0, 255))
            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)

            ar2 = ar.__getitem__ (-1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_set_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
            self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))

            ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
            self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
            self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
            self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128)))
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_contains (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            sf.set_at ((8, 8), (255, 255, 255))

            ar = pygame.PixelArray (sf)
            self.assertTrue ((0, 0, 0) in ar)
            self.assertTrue ((255, 255, 255) in ar)
            self.assertFalse ((255, 255, 0) in ar)
            self.assertFalse (0x0000ff in ar)

            # Test sliced array
            self.assertTrue ((0, 0, 0) in ar[8])
            self.assertTrue ((255, 255, 255) in ar[8])
            self.assertFalse ((255, 255, 0) in ar[8])
            self.assertFalse (0x0000ff in ar[8])
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_replace (self):
        #print "replace start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((255, 0, 0))
            rval = sf.map_rgb ((0, 0, 255))
            oval = sf.map_rgb ((255, 0, 0))
            ar = pygame.PixelArray (sf)
            ar[::2].replace ((255, 0, 0), (0, 0, 255))
            self.assertEqual (ar[0][0], rval)
            self.assertEqual (ar[1][0], oval)
            self.assertEqual (ar[2][3], rval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], rval)
            self.assertEqual (ar[9][9], oval)

            ar[::2].replace ((0, 0, 255), (255, 0, 0), weights=(10, 20, 50))
            self.assertEqual (ar[0][0], oval)
            self.assertEqual (ar[2][3], oval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], oval)
            self.assertEqual (ar[9][9], oval)
        #print "replace end"
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_2dslice_assignment (self):
        w = 2 * 5 * 8
        h = 3 * 5 * 9
        sf = pygame.Surface ((w, h), 0, 32)
        ar = pygame.PixelArray (sf)
        size = (w, h)
        strides = (1, w)
        offset = 0
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (None, None, 2)
        yslice = slice (None, None, 3)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset)
        xslice = slice (5, None, 5)
        yslice = slice (5, None, 5)
        ar, size, strides, offset = self._array_slice (
            ar, size, (xslice, yslice), strides, offset)
        self._test_assignment (sf, ar, size, strides, offset)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_shape (self):
        for shape in [[4, 16], [5, 13]]:
            w, h = shape
            sf = pygame.Surface (shape, 0, 32)
            ar = pygame.PixelArray (sf)
            ai = arrinter.ArrayInterface (ar)
            ai_shape = [ai.shape[i] for i in range(ai.nd)]
            self.assertEqual (ai_shape, shape)
            ar2 = ar[::2,:]
            ai2 = arrinter.ArrayInterface (ar2)
            w2 = len(([0] * w)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w2, h])
            ar2 = ar[:,::2]
            ai2 = arrinter.ArrayInterface (ar2)
            h2 = len(([0] * h)[::2])
            ai_shape = [ai2.shape[i] for i in range(ai2.nd)]
            self.assertEqual (ai_shape, [w, h2])
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_flags (self):
        aim = arrinter
        common_flags = (aim.PAI_NOTSWAPPED | aim.PAI_WRITEABLE |
                        aim.PAI_ALIGNED)
        s = pygame.Surface ((10, 2), 0, 32)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        ar2 = ar[::2,:]
        ai = aim.ArrayInterface (ar2)
        self.assertEqual (ai.flags, common_flags)

        s = pygame.Surface ((8, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags | aim.PAI_FORTRAN)

        s = pygame.Surface ((7, 2), 0, 24)
        ar = pygame.PixelArray (s)
        ai = aim.ArrayInterface (ar)
        self.assertEqual (ai.flags, common_flags)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_pxarray_ref (self):
        sf = pygame.Surface ((5, 5))
        ar = pygame.PixelArray (sf)
        ar2 = pygame.PixelArray (sf)

        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar, ar2))

        del ar
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar2,))

        ar = ar2[:]
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar2,))

        del ar
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (len (sf.get_locks ()), 1)
项目:pytari2600    作者:ajgrah2000    | 项目源码 | 文件源码
def __init__(self, *args):
        # 'default_color' is used by stella init, need to set before super
        self.default_color = 0
        self._colors = PygameColors()
        super(PygameStella, self).__init__(*args)

        # Test if 'PixelArray' is part of pygame
        # pygame_cffi, may not have PixelArray

        if has_numpy:
            # Use a faster blit with a numpy array, if available.      
            self.driver_draw_display = self._draw_using_numpy_array
        elif hasattr(pygame, 'PixelArray'):
            self.driver_draw_display = self._draw_using_pixel_array
        else:
            self.driver_draw_display = self._draw_using_set_at
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_pixel_array (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            if sf.mustlock():
                self.assertTrue (sf.get_locked ())

            self.assertEqual (len (ar), 10)
            del ar

            if sf.mustlock():
                self.assertFalse (sf.get_locked ())

    # Sequence interfaces
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_get_column (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((0, 0, 255))
            val = sf.map_rgb ((0, 0, 255))
            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)

            ar2 = ar.__getitem__ (-1)
            self.assertEqual (len(ar2), 8)
            self.assertEqual (ar2.__getitem__ (0), val)
            self.assertEqual (ar2.__getitem__ (1), val)
            self.assertEqual (ar2.__getitem__ (2), val)
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_get_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 255))
            for x in xrange_ (20):
                sf.set_at ((1, x), (0, 0, 11))
            for x in xrange_ (10):
                sf.set_at ((x, 1), (0, 0, 11))

            ar = pygame.PixelArray (sf)

            ar2 = ar.__getitem__ (0).__getitem__ (0)
            self.assertEqual (ar2, sf.map_rgb ((0, 0, 255)))

            ar2 = ar.__getitem__ (1).__getitem__ (0)
            self.assertEqual (ar2, sf.map_rgb ((0, 0, 11)))

            ar2 = ar.__getitem__ (-4).__getitem__ (1)
            self.assertEqual (ar2, sf.map_rgb ((0, 0, 11)))

            ar2 = ar.__getitem__ (-4).__getitem__ (5)
            self.assertEqual (ar2, sf.map_rgb ((0, 0, 255)))
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_set_pixel (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            ar.__getitem__ (0).__setitem__ (0, (0, 255, 0))
            self.assertEqual (ar[0][0], sf.map_rgb ((0, 255, 0)))

            ar.__getitem__ (1).__setitem__ (1, (128, 128, 128))
            self.assertEqual (ar[1][1], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__(-1).__setitem__ (-1, (128, 128, 128))
            self.assertEqual (ar[9][19], sf.map_rgb ((128, 128, 128)))

            ar.__getitem__ (-2).__setitem__ (-2, (128, 128, 128))
            self.assertEqual (ar[8][-2], sf.map_rgb ((128, 128, 128)))
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_contains (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            sf.set_at ((8, 8), (255, 255, 255))

            ar = pygame.PixelArray (sf)
            self.assertTrue ((0, 0, 0) in ar)
            self.assertTrue ((255, 255, 255) in ar)
            self.assertFalse ((255, 255, 0) in ar)
            self.assertFalse (0x0000ff in ar)

            # Test sliced array
            self.assertTrue ((0, 0, 0) in ar[8])
            self.assertTrue ((255, 255, 255) in ar[8])
            self.assertFalse ((255, 255, 0) in ar[8])
            self.assertFalse (0x0000ff in ar[8])
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_ass_subscript (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((255, 255, 255))
            ar = pygame.PixelArray (sf)

            # Test ellipse working
            ar[...,...] = (0, 0, 0)
            self.assertEqual (ar[0,0], 0)
            self.assertEqual (ar[1,0], 0)
            self.assertEqual (ar[-1,-1], 0)
            ar[...,] = (0, 0, 255)
            self.assertEqual (ar[0,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[1,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 0, 255)))
            ar[:,...] = (255, 0, 0)
            self.assertEqual (ar[0,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[1,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((255, 0, 0)))
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_replace (self):
        #print "replace start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((255, 0, 0))
            rval = sf.map_rgb ((0, 0, 255))
            oval = sf.map_rgb ((255, 0, 0))
            ar = pygame.PixelArray (sf)
            ar[::2].replace ((255, 0, 0), (0, 0, 255))
            self.assertEqual (ar[0][0], rval)
            self.assertEqual (ar[1][0], oval)
            self.assertEqual (ar[2][3], rval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], rval)
            self.assertEqual (ar[9][9], oval)

            ar[::2].replace ((0, 0, 255), (255, 0, 0), weights=(10, 20, 50))
            self.assertEqual (ar[0][0], oval)
            self.assertEqual (ar[2][3], oval)
            self.assertEqual (ar[3][6], oval)
            self.assertEqual (ar[8][9], oval)
            self.assertEqual (ar[9][9], oval)
        #print "replace end"
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_pxarray_ref (self):
        sf = pygame.Surface ((5, 5))
        ar = pygame.PixelArray (sf)
        ar2 = pygame.PixelArray (sf)

        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar, ar2))

        del ar
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar2,))

        ar = ar2[:]
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (sf.get_locks (), (ar2,))

        del ar
        self.assertEquals (sf.get_locked (), True)
        self.assertEquals (len (sf.get_locks ()), 1)
项目:CoolesSpiel    作者:AlinaGri    | 项目源码 | 文件源码
def createByImage(path = "map.png"):
    m       = GameMap()
    try:
        mapdata = pygame.image.load(path)
    except:
        return m
    size    = mapdata.get_size()
    black   = mapdata.map_rgb((0, 0, 0))
    mapdata = pygame.PixelArray(mapdata)
    m.setWidth(size[0])
    m.setHeight(size[1])
    for x in xrange(size[0]):
        for y in xrange(size[1]):
            if mapdata[x][y] == black:
                m._invalidPositions.add((x, y))
    return m
项目:sc8pr    作者:dmaccarthy    | 项目源码 | 文件源码
def apply(self, img, n=0):
        srf = self.srfSize(img)
        d = self.dist * (1 - n)
        pygame.PixelArray(srf).replace(self.color1, self.color2, d)
        return img
项目:sc8pr    作者:dmaccarthy    | 项目源码 | 文件源码
def apply(self, img, n):
        "Apply pixel-by-pixel effect"
        srf = self.srfSize(img)
        pxa = PixelArray(srf)
        x = 0
        for pxCol in pxa:
            for y in range(len(pxCol)):
                c = pxCol[y]
                px = self.pixel(n, x, y, c)
                if px != c: pxCol[y] = px
            x += 1
        return srf
项目:sc8pr    作者:dmaccarthy    | 项目源码 | 文件源码
def __init__(self, colors=None):
        img = Image.fromBytes(sc8prData("robot"))
        if colors:
            px = pygame.PixelArray(img.image)
            for i in range(2):
                c = 255 * (1 - i), 0, 255*i
                px.replace(c, rgba(colors[i]))
        img = img.tiles(2)
        super().__init__(img)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_compare(self):
        # __doc__ (as of 2008-06-25) for pygame.pixelarray.PixelArray.compare:

          # PixelArray.compare (array, distance=0, weights=(0.299, 0.587, 0.114)): Return PixelArray
          # Compares the PixelArray with another one.

        w = 10
        h = 20
        size = w, h
        sf = pygame.Surface (size, 0, 32)
        ar = pygame.PixelArray (sf)
        sf2 = pygame.Surface (size, 0, 32)
        self.assertRaises (TypeError, ar.compare, sf2)
        ar2 = pygame.PixelArray (sf2)
        ar3 = ar.compare (ar2)
        self.assert_ (isinstance (ar3, pygame.PixelArray))
        self.assertEqual (ar3.shape, size)
        sf2.fill (pygame.Color ('white'))
        self.assert_surfaces_equal (sf2, ar3.surface)
        del ar3
        r = pygame.Rect (2, 5, 6, 13)
        sf.fill (pygame.Color ('blue'), r)
        sf2.fill (pygame.Color ('red'))
        sf2.fill (pygame.Color ('blue'), r)
        ar3 = ar.compare (ar2)
        sf.fill (pygame.Color ('white'), r)
        self.assert_surfaces_equal (sf, ar3.surface)

        # FINISH ME!
        # Test other bit depths, slices, and distance != 0.
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_as_class (self):
        # Check general new-style class freatures.
        sf = pygame.Surface ((2, 3), 0, 32)
        ar = pygame.PixelArray (sf)
        self.assertRaises (AttributeError, getattr, ar, 'nonnative')
        ar.nonnative = 'value'
        self.assertEqual (ar.nonnative, 'value')
        self.assertTrue ('nonnative' in ar.__dict__)
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None)

        class C (pygame.PixelArray):
            def __str__ (self):
                return "string (%i, %i)" % self.shape

        ar = C (sf)
        self.assertEqual (str (ar), "string (2, 3)")
        r = weakref.ref (ar)
        self.assertTrue (r() is ar)
        del ar
        gc.collect ()
        self.assertTrue (r() is None)

    # Sequence interfaces
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_get_slice (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            self.assertEqual (len (ar[0:2]), 2)
            self.assertEqual (len (ar[3:7][3]), 20)

            self.assertEqual (ar[0:0], None)
            self.assertEqual (ar[5:5], None)
            self.assertEqual (ar[9:9], None)

            # Has to resolve to ar[7:8]
            self.assertEqual (len (ar[-3:-2]), 1)      # 2D
            self.assertEqual (len (ar[-3:-2][0]), 20)  # 1D

            # Try assignments.

            # 2D assignment.
            ar[2:5] = (255, 255, 255)

            # 1D assignment
            ar[3][3:7] = (10, 10, 10)
            self.assertEqual (ar[3][5], sf.map_rgb ((10, 10, 10)))
            self.assertEqual (ar[3][6], sf.map_rgb ((10, 10, 10)))
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_get_surface (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)
            self.assert_ (ar.surface is sf)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_ass_subscript (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((6, 8), 0, bpp)
            sf.fill ((255, 255, 255))
            ar = pygame.PixelArray (sf)

            # Test ellipse working
            ar[...,...] = (0, 0, 0)
            self.assertEqual (ar[0,0], 0)
            self.assertEqual (ar[1,0], 0)
            self.assertEqual (ar[-1,-1], 0)
            ar[...,] = (0, 0, 255)
            self.assertEqual (ar[0,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[1,0], sf.map_rgb ((0, 0, 255)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 0, 255)))
            ar[:,...] = (255, 0, 0)
            self.assertEqual (ar[0,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[1,0], sf.map_rgb ((255, 0, 0)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((255, 0, 0)))
            ar[...] = (0, 255, 0)
            self.assertEqual (ar[0,0], sf.map_rgb ((0, 255, 0)))
            self.assertEqual (ar[1,0], sf.map_rgb ((0, 255, 0)))
            self.assertEqual (ar[-1,-1], sf.map_rgb ((0, 255, 0)))

            # Ensure x and y are freed for array[x, y] = p
            # Bug fix: reference counting
            if hasattr(sys, 'getrefcount'):
                class Int(int):
                    """Unique int instances"""
                    pass

                sf = pygame.Surface ((2, 2), 0, 32)
                ar = pygame.PixelArray (sf)
                x, y = Int(0), Int(1)
                rx_before, ry_before = sys.getrefcount (x), sys.getrefcount (y)
                ar[x, y] = 0
                rx_after, ry_after = sys.getrefcount (x), sys.getrefcount (y)
                self.assertEqual (rx_after, rx_before)
                self.assertEqual (ry_after, ry_before)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_pixels_field(self):
        for bpp in [1, 2, 3, 4]:
            sf = pygame.Surface ((11, 7), 0, bpp * 8)
            ar = pygame.PixelArray (sf)
            ar2 = ar[1:,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.itemsize)
            ar2 = ar[:,1:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[1])
            ar2 = ar[::-1,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.itemsize)
            ar2 = ar[::-2,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.itemsize)
            ar2 = ar[:,::-1]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[1] - 1) * ar.strides[1])
            ar3 = ar2[::-1,:]
            self.assertEqual (ar3._pixels_address - ar._pixels_address,
                              (ar.shape[0] - 1) * ar.strides[0] +
                              (ar.shape[1] - 1) * ar.strides[1])
            ar2 = ar[:,::-2]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              (ar.shape[1] - 1) * ar.strides[1])
            ar2 = ar[2::,3::]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 2 + ar.strides[1] * 3)
            ar2 = ar[2::2,3::4]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 2 + ar.strides[1] * 3)
            ar2 = ar[9:2:-1,:]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[0] * 9)
            ar2 = ar[:,5:2:-1]
            self.assertEqual (ar2._pixels_address - ar._pixels_address,
                              ar.strides[1] * 5)
            ##? ar2 = ar[:,9:2:-1]
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_iter (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((5, 10), 0, bpp)
            ar = pygame.PixelArray (sf)
            iterations = 0
            for col in ar:
                self.assertEqual (len (col), 10)
                iterations += 1
            self.assertEqual (iterations, 5)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_extract (self):
        #print "extract start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((0, 0, 255))
            sf.fill ((255, 0, 0), (2, 2, 6, 6))

            white = sf.map_rgb ((255, 255, 255))
            black = sf.map_rgb ((0, 0, 0))

            ar = pygame.PixelArray (sf)
            newar = ar.extract ((255, 0, 0))

            self.assertEqual (newar[0][0], black)
            self.assertEqual (newar[1][0], black)
            self.assertEqual (newar[2][3], white)
            self.assertEqual (newar[3][6], white)
            self.assertEqual (newar[8][9], black)
            self.assertEqual (newar[9][9], black)

            newar = ar.extract ((255, 0, 0), weights=(10, 0.1, 50))
            self.assertEqual (newar[0][0], black)
            self.assertEqual (newar[1][0], black)
            self.assertEqual (newar[2][3], white)
            self.assertEqual (newar[3][6], white)
            self.assertEqual (newar[8][9], black)
            self.assertEqual (newar[9][9], black)
        #print "extract end"
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_array_properties(self):
        # itemsize, ndim, shape, and strides.
        for bpp in [1, 2, 3, 4]:
            sf = pygame.Surface ((2, 2), 0, bpp * 8)
            ar = pygame.PixelArray (sf)
            self.assertEqual (ar.itemsize, bpp)

        for shape in [(4, 16), (5, 13)]:
            w, h = shape
            sf = pygame.Surface (shape, 0, 32)
            bpp = sf.get_bytesize ()
            pitch = sf.get_pitch ()
            ar = pygame.PixelArray (sf)
            self.assertEqual (ar.ndim, 2)
            self.assertEqual (ar.shape, shape)
            self.assertEqual (ar.strides, (bpp, pitch))
            ar2 = ar[::2,:]
            w2 = len(([0] * w)[::2])
            self.assertEqual (ar2.ndim, 2)
            self.assertEqual (ar2.shape, (w2, h))
            self.assertEqual (ar2.strides, (2 * bpp, pitch))
            ar2 = ar[:,::2]
            h2 = len(([0] * h)[::2])
            self.assertEqual (ar2.ndim, 2)
            self.assertEqual (ar2.shape, (w, h2))
            self.assertEqual (ar2.strides, (bpp, 2 * pitch))
            ar2 = ar[1]
            self.assertEqual (ar2.ndim, 1)
            self.assertEqual (ar2.shape, (h,))
            self.assertEqual (ar2.strides, (pitch,))
            ar2 = ar[:,1]
            self.assertEqual (ar2.ndim, 1)
            self.assertEqual (ar2.shape, (w,))
            self.assertEqual (ar2.strides, (bpp,))
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_color_value (self):
        # Confirm that a PixelArray slice assignment distinguishes between
        # pygame.Color and tuple objects as single (r, g, b[, a]) colors
        # and other sequences as sequences of colors to be treated as
        # slices.
        sf = pygame.Surface ((5, 5), 0, 32)
        ar = pygame.PixelArray (sf)
        index = slice(None, None, 1)
        ar.__setitem__ (index, (1, 2, 3))
        self.assertEqual (ar[0, 0], sf.map_rgb ((1, 2, 3)))
        ar.__setitem__ (index, pygame.Color (10, 11, 12))
        self.assertEqual (ar[0, 0], sf.map_rgb ((10, 11, 12)))
        self.assertRaises (ValueError, ar.__setitem__, index, (1, 2, 3, 4, 5))
        self.assertRaises (ValueError, ar.__setitem__, (index, index),
                           (1, 2, 3, 4, 5))
        self.assertRaises (ValueError, ar.__setitem__, index, [1, 2, 3])
        self.assertRaises (ValueError, ar.__setitem__, (index, index),
                           [1, 2, 3])
        sf = pygame.Surface ((3, 3), 0, 32)
        ar = pygame.PixelArray (sf)
        ar[:] = (20, 30, 40)
        self.assertEqual (ar[0, 0], sf.map_rgb ((20, 30, 40)))
        ar[:] = [20, 30, 40]
        self.assertEqual (ar[0, 0], 20)
        self.assertEqual (ar[1, 0], 30)
        self.assertEqual (ar[2, 0], 40)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_transpose (self):
        # PixelArray.transpose(): swap axis on a 2D array, add a length
        # 1 x axis to a 1D array.
        sf = pygame.Surface ((3, 7), 0, 32)
        ar = pygame.PixelArray (sf)
        w, h = ar.shape
        dx, dy = ar.strides
        for i in range (w * h):
            x = i % w
            y = i // w
            ar[x, y] = i
        ar_t = ar.transpose()
        self.assertEqual (ar_t.shape, (h, w))
        self.assertEqual (ar_t.strides, (dy, dx))
        for i in range (w * h):
            x = i % w
            y = i // w
            self.assertEqual (ar_t[y, x], ar[x, y])
        ar1D = ar[0]
        ar2D = ar1D.transpose()
        self.assertEqual (ar2D.shape, (1, h))
        for y in range (h):
            self.assertEqual (ar1D[y], ar2D[0, y])
        ar1D = ar[:,0]
        ar2D = ar1D.transpose()
        self.assertEqual (ar2D.shape, (1, w))
        for x in range (2):
            self.assertEqual (ar1D[x], ar2D[0, x])
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_length_1_dimension_broadcast (self):
        w = 5
        sf = pygame.Surface ((w, w), 0, 32)
        ar = pygame.PixelArray (sf)
        # y-axis broadcast.
        sf_x = pygame.Surface ((w, 1), 0, 32)
        ar_x = pygame.PixelArray (sf_x)
        for i in range (w):
            ar_x[i, 0] = (w + 1) * 10
        ar[...] = ar_x
        for y in range (w):
            for x in range (w):
                self.assertEqual (ar[x, y], ar_x[x, 0])
        # x-axis broadcast.
        ar[...] = 0
        sf_y = pygame.Surface ((1, w), 0, 32)
        ar_y = pygame.PixelArray (sf_y)
        for i in range (w):
            ar_y[0, i] = (w + 1) * 10
        ar[...] = ar_y
        for x in range (w):
            for y in range (w):
                self.assertEqual (ar[x, y], ar_y[0, y])
        # (1, 1) array broadcast.
        ar[...] = 0
        sf_1px = pygame.Surface ((1, 1), 0, 32)
        ar_1px = pygame.PixelArray (sf_1px)
        ar_1px[0, 0] = 42  # Well it had to show up somewhere.
        ar[...] = ar_1px
        for y in range (w):
            for x in range (w):
                self.assertEqual (ar[x, y], 42)
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_assign_size_mismatch (self):
        sf = pygame.Surface ((7, 11), 0, 32)
        ar = pygame.PixelArray (sf)
        self.assertRaises (ValueError, ar.__setitem__, Ellipsis, ar[:, 0:2])
        self.assertRaises (ValueError, ar.__setitem__, Ellipsis, ar[0:2, :])
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_repr (self):
        # Python 3.x bug: the tp_repr slot function returned NULL instead
        # of a Unicode string, triggering an exception.
        sf = pygame.Surface ((3, 1), pygame.SRCALPHA, 16)
        ar = pygame.PixelArray(sf)
        ar[...] = 42
        pixel = sf.get_at_mapped ((0, 0))
        self.assertEqual(repr (ar),
                         type (ar).__name__ + "([\n  [42, 42, 42]]\n)")
项目:Projects    作者:it2school    | 项目源码 | 文件源码
def test_itemsize (self):
        for bytes_per_pixel in range(1, 5):
            bits_per_pixel = 8 * bytes_per_pixel
            sf = pygame.Surface ((2, 2), 0, bits_per_pixel)
            ar = pygame.PixelArray (sf)
            ai = arrinter.ArrayInterface (ar)
            self.assertEqual (ai.itemsize, bytes_per_pixel)
项目:PyCut    作者:FOSSRIT    | 项目源码 | 文件源码
def addMessage(self, message=None, message_arr=None, color=None):
        old_messages = self.messages
        for msg in self.messages:
            arr = pygame.PixelArray(msg)
            arr.replace(self.color, self.old_message_color)
        if not(color):
            color = self.color
        if message:
            self.messages.append(self.pen.render(message, True, color))
        if message_arr:
            for msg in message_arr:
                self.messages.append(self.pen.render(msg, True, color))
        self.dirty = True
项目:pytari2600    作者:ajgrah2000    | 项目源码 | 文件源码
def _draw_using_pixel_array(self):
        pxarray = pygame.PixelArray(self._background)
        for y in range(min(self.END_DRAW_Y - self.START_DRAW_Y, self.FRAME_HEIGHT)):
            for x in range(self.FRAME_WIDTH):
                pxarray[x][y] = self._display_lines[y][x]
        del pxarray
项目:F1Dash    作者:ctrmint    | 项目源码 | 文件源码
def pix_Array():
    # get a pixel array of the surface
    global pixArray
    pixArray = pygame.PixelArray(windowSurface)
    #pixArray[480][380] = BLACK
    pixArray[480][800] = BLACK
    del pixArray
    return
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def todo_test_compare(self):
        # __doc__ (as of 2008-06-25) for pygame.pixelarray.PixelArray.compare:

          # PixelArray.compare (array, distance=0, weights=(0.299, 0.587, 0.114)): Return PixelArray
          # Compares the PixelArray with another one.

        self.fail()
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_get_slice (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)

            if PY3:
                self.assertEqual (len (ar[0:2]), 2)
                self.assertEqual (len (ar[3:7][3]), 20)

                self.assertEqual (ar[0:0], None)
                self.assertEqual (ar[5:5], None)
                self.assertEqual (ar[9:9], None)
            else:
                self.assertEqual (len (ar.__getslice__ (0, 2)), 2)
                self.assertEqual (len (ar.__getslice__ (3, 7)[3]), 20)

                self.assertEqual (ar.__getslice__ (0, 0), None)
                self.assertEqual (ar.__getslice__ (5, 5), None)
                self.assertEqual (ar.__getslice__ (9, 9), None)

            # Has to resolve to ar[7:8]
            self.assertEqual (len (ar[-3:-2]), 20)

            # Try assignments.

            # 2D assignment.
            if PY3:
                ar[2:5] = (255, 255, 255)
            else:
                ar.__setslice__ (2, 5, (255, 255, 255))
            self.assertEqual (ar[3][3], sf.map_rgb ((255, 255, 255)))

            # 1D assignment
            if PY3:
                ar[3][3:7] = (10, 10, 10)
            else:
                ar[3].__setslice__ (3, 7, (10, 10, 10))

            self.assertEqual (ar[3][5], sf.map_rgb ((10, 10, 10)))
            self.assertEqual (ar[3][6], sf.map_rgb ((10, 10, 10)))
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_get_surface (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((0, 0, 0))
            ar = pygame.PixelArray (sf)
            self.assertEqual (sf, ar.surface)
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_make_surface (self):
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 20), 0, bpp)
            sf.fill ((255, 255, 255))
            ar = pygame.PixelArray (sf)
            newsf = ar[::2,::2].make_surface ()
            rect = newsf.get_rect ()
            self.assertEqual (rect.width, 5)
            self.assertEqual (rect.height, 10)
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def test_extract (self):
        #print "extract start"
        for bpp in (8, 16, 24, 32):
            sf = pygame.Surface ((10, 10), 0, bpp)
            sf.fill ((0, 0, 255))
            sf.fill ((255, 0, 0), (2, 2, 6, 6))

            white = sf.map_rgb ((255, 255, 255))
            black = sf.map_rgb ((0, 0, 0))

            ar = pygame.PixelArray (sf)
            newar = ar.extract ((255, 0, 0))

            self.assertEqual (newar[0][0], black)
            self.assertEqual (newar[1][0], black)
            self.assertEqual (newar[2][3], white)
            self.assertEqual (newar[3][6], white)
            self.assertEqual (newar[8][9], black)
            self.assertEqual (newar[9][9], black)

            newar = ar.extract ((255, 0, 0), weights=(10, 0.1, 50))
            self.assertEqual (newar[0][0], black)
            self.assertEqual (newar[1][0], black)
            self.assertEqual (newar[2][3], white)
            self.assertEqual (newar[3][6], white)
            self.assertEqual (newar[8][9], black)
            self.assertEqual (newar[9][9], black)
        #print "extract end"
项目:AIFun    作者:Plottel    | 项目源码 | 文件源码
def todo_test_surface(self):

        # __doc__ (as of 2008-08-02) for pygame.pixelarray.PixelArray.surface:

          # PixelArray.surface: Return Surface
          # Gets the Surface the PixelArray uses.
          # 
          # The Surface, the PixelArray was created for. 

        self.fail()
项目:text-renderer    作者:cjnolet    | 项目源码 | 文件源码
def add_colour(self, canvas, fg_surf, border_surf=None):
        cs = self.colourstate.get_sample(2 + (border_surf is not None))
        # replace background
        pygame.PixelArray(canvas).replace((255,255,255), (cs[0],cs[0],cs[0]), distance=1.0)
        # replace foreground
        pygame.PixelArray(fg_surf).replace((0,0,0), (cs[1],cs[1],cs[1]), distance=0.99)
项目:Playing-Falldown-with-a-DQN    作者:HarrySonghurst    | 项目源码 | 文件源码
def format_surface_render(self, surface):
        resized_surface = pygame.transform.smoothscale(pygame.PixelArray(surface).make_surface(), (52, 68))  # (W, H)
        formatted_array = np.zeros((68, 52), dtype="uint8")
        for i in range(68):
            for j in range(52):
                red, green, blue, alpha = resized_surface.get_at((j, i))  # (x, y)
                formatted_array[i, j] = int((red + green + blue) / 3)
        return formatted_array