本文整理汇总了Python中pygame.surfarray方法的典型用法代码示例。如果您正苦于以下问题:Python pygame.surfarray方法的具体用法?Python pygame.surfarray怎么用?Python pygame.surfarray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygame
的用法示例。
在下文中一共展示了pygame.surfarray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: addNoiseAndGray
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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
示例2: slow_down_sound
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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
示例3: test_array3d
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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))
示例4: test_map_array
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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))
示例5: test_pixels2d
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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))
示例6: getCurrentFrame
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
def getCurrentFrame(self):
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
return image_data
示例7: _on_screen_update
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
def _on_screen_update(self, _, *args, **kwargs):
surface_array = pygame.surfarray.array3d(pygame.display.get_surface())
reward, terminal = self.get_feedback()
keys = self.get_keys_pressed(surface_array, reward, terminal)
self._last_keys_pressed = self._keys_pressed
self._keys_pressed = keys
# now we have processed a frame increment the game timer
self._game_time += self.get_ms_per_frame()
示例8: refresh
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
def refresh(self):
if "enable_ui_recording" in self.env.config:
screen_to_dump = cv2.cvtColor(self.screen_arr.transpose(1, 0, 2), cv2.COLOR_BGR2RGB)
cv2.imshow("Recording", screen_to_dump)
cmd=cv2.waitKey(5)%256
if cmd == ord('r'):
self.start_record()
if cmd == ord('q'):
self.end_record()
if self.is_recording:
#self.curr_output.write(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#from IPython import embed; embed()
cv2.imwrite(os.path.join(self.output_dir, "frame%06d.png" % self.record_nframe), screen_to_dump)
self.record_nframe += 1
#self.curr_output.write(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
#with Profiler("Refreshing"):
pygame.display.flip()
surfarray.blit_array(self.screen, self.screen_arr)
#print(self.screen_arr.shape)
screen_to_dump = cv2.cvtColor(self.screen_arr.transpose(1,0,2), cv2.COLOR_BGR2RGB)
screen = pickle.dumps(cv2.imencode('.jpg', screen_to_dump), protocol=0)
self.socket.send(b"ui" + screen)
#surf = pygame.surfarray.make_surface(self.screen_arr)
#self.screen.blit(surf, (0, 0))
#pygame.display.update()
示例9: setUp
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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)
示例10: test_array2d
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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()))
示例11: test_get_arraytype
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
def test_get_arraytype(self):
array_type = pygame.surfarray.get_arraytype()
self.assertEqual(array_type, 'numpy',
"unknown array type %s" % array_type)
示例12: test_get_arraytypes
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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)
示例13: test_make_surface
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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])))
示例14: _test_pixels_rgb
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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))
示例15: test_use_arraytype
# 需要导入模块: import pygame [as 别名]
# 或者: from pygame import surfarray [as 别名]
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')