def runTest(self) -> None: width, height = 800, 600 screen = pygame.display.set_mode((width * 2, height)) pygame.display.set_caption("bright pass filter (bpf24_inplace)") checker = pygame.image.load( '../Assets/background_checker.png').convert() checker = pygame.transform.smoothscale(checker, (1600, 600)) background = pygame.image.load('../Assets/I1.png').convert_alpha() background = pygame.transform.smoothscale(background, (800, 600)) background_cp = background.copy() # array3d # test_bpf24_inplace(array3d(background), 40) # pixel3d test_bpf24_inplace(pixels3d(background), 45) # test argument threshold self.assertRaises(OverflowError, test_bpf24_inplace, array3d(background), -40) self.assertRaises(OverflowError, test_bpf24_inplace, array3d(background), 440) display(screen, checker, background_cp, background)
def runTest(self) -> None: width, height = 800, 600 screen = pygame.display.set_mode((width * 2, height)) pygame.display.set_caption("Blur5x5Array24") checker = pygame.image.load( '../Assets/background_checker.png').convert() checker = pygame.transform.smoothscale(checker, (1600, 600)) background = pygame.image.load('../Assets/Aliens.jpg').convert() background = pygame.transform.smoothscale(background, (800, 600)) # Basic checks blurring an array with only 255 values # Full array with 255 values full_255 = numpy.full((800, 600, 3), (255, 255, 255), numpy.uint8) blur_array = blur5x5_array24(full_255) self.assertTrue(numpy.array_equal(blur_array, full_255)) # Basic checks blurring an array with only 0 values full_0 = numpy.full((800, 600, 3), (0, 0, 0), numpy.uint8) blur_array = blur5x5_array24(full_0) self.assertTrue(numpy.array_equal(blur_array, full_0)) self.assertEqual(background.get_bytesize(), 4) rgb_array = array3d(background) blur_rgb_array = blur5x5_array24(rgb_array) self.assertIsInstance(blur_rgb_array, numpy.ndarray) background = pygame.image.load('../Assets/Aliens.jpg').convert_alpha() background = pygame.transform.smoothscale(background, (800, 600)) rgb_array = array3d(background) blur_rgb_array = blur5x5_array24(rgb_array) w, h, d = blur_rgb_array.shape # Check if the array kept the same dimensions width and height # Check also that the alpha channel as been removed self.assertTrue(d == 3) self.assertTrue(w == 800) self.assertTrue(h == 600) self.assertIsInstance(blur_rgb_array, numpy.ndarray) blur_surface = make_surface(blur_rgb_array) # Array shape (w, h, 3) uint8 rgb_array = numpy.zeros((800, 600, 3), numpy.uint8) blur5x5_array24(rgb_array) # Array shape (w, h, 4) uint8 rgb_array = numpy.zeros((800, 600, 4), numpy.uint8) blur5x5_array24(rgb_array) # Testing wrong datatype rgb_array = numpy.zeros((800, 600, 3), numpy.float32) self.assertRaises(ValueError, blur5x5_array24, rgb_array) # Testing wrong datatype rgb_array = numpy.zeros((800, 600, 3), numpy.int8) self.assertRaises(ValueError, blur5x5_array24, rgb_array) display(screen, checker, background, blur_surface)
def drawPlayerPieces(self): self.piecebox.fill((255, 255, 255)) pieceImg = image.load(join("sprites", "piecesmall.jpg")) pieceArrays = { "r": surfarray.array3d(pieceImg), "g": surfarray.array3d(pieceImg), "b": surfarray.array3d(pieceImg), "y": surfarray.array3d(pieceImg) } pieceArrays["r"][:, :, 1:] = 0 pieceArrays["g"][:, :, [0, 2]] = 0 pieceArrays["b"][:, :, :2] = 0 pieceArrays["y"][:, :, 2] = 0 bpos = n.array((0, 0)) for player in o.players.players: surfarray.blit_array(pieceImg, pieceArrays[player.c]) for size in player.pieces.keys(): for p in player.pieces[size]: if len(p.m) < len(p.m[0]): isTaller = True h = len(p.m) psize = (9 * len(p.m[0]), 9 * len(p.m)) else: isTaller = False h = len(p.m[0]) psize = (9 * len(p.m), 9 * len(p.m[0])) pieceImg.set_alpha(100) if player is o.players.cur: pieceRect = self.piecebox.subsurface(Rect(bpos, psize)) o.players.pieces.append((pieceRect, size, p)) if p is player.curPiece: pieceImg.set_alpha(255) for r in range(len(p.m)): for c in range(len(p.m[0])): if isTaller: x = c y = r else: x = r y = c if p.m[r][c] == 1: pos = n.array((x * 9, y * 9)) if player.c == o.players.cur.c: pieceRect.blit(pieceImg, pos) else: self.piecebox.blit(pieceImg, bpos + pos) bpos[1] += h * 9 + 1 bpos[0] += 50 bpos[1] = 0
def runTest(self) -> None: width, height = 800, 600 screen = pygame.display.set_mode((width * 2, height)) pygame.display.set_caption("filter24_inplace") checker = pygame.image.load( '../Assets/background_checker.png').convert() checker = pygame.transform.smoothscale(checker, (1600, 600)) background = pygame.image.load('../Assets/I2.png').convert() background = pygame.transform.smoothscale(background, (800, 600)) background_cp = background.copy() mask_image = pygame.image.load( '../Assets/color_mask_circle.png').convert() mask_image = pygame.transform.smoothscale(mask_image, (800, 600)) mask_array = build_mask_from_surface(mask_image, invert_mask=False) filtering24(background, mask_array) display(screen, checker, background_cp, background) background = pygame.image.load('../Assets/I2.png').convert() background = pygame.transform.smoothscale(background, (800, 600)) background_cp = background.copy() mask_array = build_mask_from_surface(background, invert_mask=True) filtering24(background, mask_array) pygame.display.set_caption( "filter24_inplace with mask invert_mask=True") display(screen, checker, background_cp, background) # Array filled with 1.0 test_mask = numpy.full((800, 600), 1.0, numpy.float32) background = pygame.image.load('../Assets/I2.png').convert() background = pygame.transform.smoothscale(background, (800, 600)) filtering24(background, test_mask) arr1 = array3d(background) arr2 = array3d(background_cp) self.assertTrue(numpy.array_equal(arr1, arr2)) # display(screen, checker, background_cp, background) # Array filled with 0.0 test_mask = numpy.full((800, 600), 0.0, numpy.float32) background = pygame.image.load('../Assets/I2.png').convert() background = pygame.transform.smoothscale(background, (800, 600)) filtering24(background, test_mask) arr1 = array3d(background) self.assertTrue(numpy.sum(arr1) == 0)
def outlineEdges(surface, laplacian_filter = numpy.array([[0,1,0],[1,-4,1],[0,1,0]])): gray_image = numpy.mean(surfarray.array3d(surface), 2) edges = signal.convolve2d(gray_image, laplacian_filter, mode="same") surf = surfarray.make_surface(edges) return surf
def gray_scale(img): arr = surfarray.array3d(img) # luminosity filter avgs = [[(r * 0.298 + g * 0.587 + b * 0.114) for (r, g, b) in col] for col in arr] arr = np.array([[[avg, avg, avg] for avg in col] for col in avgs]) return surfarray.make_surface(arr)
def draw(self, surface, model): # draw rectangles to display canvas for x, y in np.ndindex(model.world.shape): rect_x = x * self.scale rect_xa = rect_x - (self.scale - 1) rect_y = y * self.scale rect_ya = rect_y - (self.scale - 1) rect_shape = [rect_xa, rect_ya, rect_x, rect_y] if model.world[x, y] > 0: pygame.draw.rect(surface, (21, 128, 0), rect_shape) else: pygame.draw.rect(surface, (0, 0, 0), rect_shape) # apply post processing to display canvas rgbarray = surfarray.array3d(surface) factor = np.array((8,), np.int32) soften = np.array(rgbarray, np.int32) soften[1:, :] += rgbarray[:-1, :] * factor soften[:-1, :] += rgbarray[1:, :] * factor soften[:, 1:] += rgbarray[:, :-1] * factor soften[:, :-1] += rgbarray[:, 1:] * factor soften //= 16 screen = pygame.display.set_mode(soften. shape[:2], 0, 32) surfarray.blit_array(screen, soften)
def process(self): """Carry out the image capture""" logging.debug("Running Video capture process") # starts the camera self.camera.start() while True: obj = self.input_channel.get(True) # this is blocking if obj.last: logging.info("We have finished capturing from the webcam.") self.stop = True self.output_channel.put(obj) return tag = obj['tag'] try: surface = self.camera.get_image(self.snapshot) except: surface = self.camera.get_image() #Convert the image to a numpy array image = surfarray.array3d(surface) if self.grey: # convert to grayscale numpy array for image processing image = numpy.mean(image, 2) self.output_channel.put(Event(tag, image)) logging.debug("Video Snapshot process added data at tag: %s" % tag)
def Desaturate(surface): pixelData = surfarray.array3d(surface) pixelData = pixelData.dot([0.298, 0.587, 0.114])[:, :, None].repeat(3, axis=2) return surfarray.make_surface(pixelData)
def edgeDetect(surf): gray_image = numpy.mean(surfarray.array3d(surf), 2) edges = signal.convolve2d(gray_image, laplacian_filter, mode="same") surf = surfarray.make_surface(edges) return surf
def gaussian_blur(surface, std, strength): """ Blur the given surface Returns the modified surface """ matrix = surfarray.array3d(surface) width = matrix.shape[1] // 2 def in_gkernel(): # generate one dimensional kernel drange = lambda x: range(-x, 1 + x) return [np.exp(-std * abs(i)) for i in drange(width)] kern_l = in_gkernel() kernel = np.array(kern_l) / np.sum(kern_l) in_pad = lambda x: np.pad(x, width, mode="edge") in_cnv = lambda x: np.convolve(in_pad(x), kernel, mode="valid") for i in range(matrix.shape[1]): matrix[i, :, 0] = in_cnv(matrix[i, :, 0]) matrix[i, :, 1] = in_cnv(matrix[i, :, 1]) matrix[i, :, 2] = in_cnv(matrix[i, :, 2]) for j in range(matrix.shape[0]): matrix[:, j, 0] = in_cnv(matrix[:, j, 0]) matrix[:, j, 1] = in_cnv(matrix[:, j, 1]) matrix[:, j, 2] = in_cnv(matrix[:, j, 2]) return surfarray.make_surface(matrix)
def __init__(self, screen_size=200, view_size=100, prey_num=1, predator_num=4): self.viewer = None self.canvas = pygame.Surface((CUBE_SIZE, CUBE_SIZE)) self.screen = sarray.array3d(self.canvas) self.screen_size = 200 self.prey_num = 1 self.predator_num = predator_num self.prey = [] self.predator = [] self.reward = 0 self.prey_reward = [] self.predator_reward = [] self.rng = np.random.RandomState() self.view_size = view_size self.screen_size = screen_size for i in range(prey_num): self.prey.append( Actor(role="prey_{}".format(i), pos=[ np.random.randint(0, self.screen_size), np.random.randint(0, self.screen_size) ], vel=[0, 0])) for i in range(predator_num): self.predator.append( Actor(role="predator_{}".format(i), pos=[ np.random.randint(0, self.screen_size), np.random.randint(0, self.screen_size) ], vel=[0, 0]))
def createCharTexture(self, char, mode=None): """Create character's texture/bitmap as a Numeric array w/ width and height This uses PyGame and Numeric to attempt to create an anti-aliased luminance texture-map of the given character and return it as (data, width, height). """ try: letter_render = self.font.render(char, 1, (255, 255, 255)) except: traceback.print_exc() return None, font.CharacterMetrics(char, 0, 0) else: # XXX Figure out why this rotate appears to be required :( letter_render = transform.rotate(letter_render, -90.0) colour = surfarray.array3d(letter_render) alpha = surfarray.array_alpha(letter_render) colour[:, :, 1] = alpha colour = colour[:, :, :2] colour = contiguous(colour).astype('B') # This produces what looks like garbage, but displays correctly colour.shape = ( colour.shape[1], colour.shape[0], ) + colour.shape[2:] return colour, font.CharacterMetrics( char, colour.shape[0], colour.shape[1], )
def createCharTexture( self, char, mode=None ): """Create character's texture/bitmap as a Numeric array w/ width and height This uses PyGame and Numeric to attempt to create an anti-aliased luminance texture-map of the given character and return it as (data, width, height). """ try: letter_render = self.font.render( char, 1, (255,255,255) ) except: traceback.print_exc() return None, font.CharacterMetrics(char,0,0) else: # XXX Figure out why this rotate appears to be required :( letter_render = transform.rotate( letter_render, -90.0) colour = surfarray.array3d( letter_render ) alpha = surfarray.array_alpha( letter_render ) colour[:,:,1] = alpha colour = colour[:,:,:2] colour = contiguous( colour ) # This produces what looks like garbage, but displays correctly colour.shape = (colour.shape[1],colour.shape[0],)+colour.shape[2:] return colour, font.CharacterMetrics( char, colour.shape[0], colour.shape[1], )
def __init__(self, x_pos: int, y_pos: int, width: int, height: int, background: Tuple[int, int, int], text_size: int, text_color: Tuple[int, int, int], value: str, closed: bool = True): Sprite.__init__(self) self.value = value self.image = Surface([width, height]) self.image.fill(background) image_array = np.array(surfarray.array3d(self.image)) if closed: image_array[:, :5, :] = np.zeros((1, 1, 1)) image_array[:, -5:, :] = np.zeros((1, 1, 1)) image_array[:5, :, :] = np.zeros((1, 1, 1)) image_array[-5:, :, :] = np.zeros((1, 1, 1)) border_image = surfarray.make_surface(image_array) self.image = border_image x, y = render_inline_text( self.image, value, text_size, text_color, start_x=UIDropdownMenu.Selector.BORDER_MARGIN, end_x=round(width * 0.8), start_y=UIDropdownMenu.Selector.BORDER_MARGIN, end_y=height - UIDropdownMenu.Selector.BORDER_MARGIN) self.rect = self.image.get_rect() self.rect.topleft = (x_pos, y_pos) self.rect.height = y
def next_frame(self, action): pump() reward = 0.1 terminal = False # Check input action if action == 1: self.current_velocity_y = self.upward_speed self.is_flapped = True # Update score bird_center_x = self.bird_x + self.bird_width / 2 for pipe in self.pipes: pipe_center_x = pipe["x_upper"] + self.pipe_width / 2 if pipe_center_x < bird_center_x < pipe_center_x + 5: self.score += 1 reward = 1 break # Update index and iteration if (self.iter + 1) % 3 == 0: self.bird_index = next(self.bird_index_generator) self.iter = 0 self.base_x = -((-self.base_x + 100) % self.base_shift) # Update bird's position if self.current_velocity_y < self.max_velocity_y and not self.is_flapped: self.current_velocity_y += self.downward_speed if self.is_flapped: self.is_flapped = False self.bird_y += min(self.current_velocity_y, self.bird_y - self.current_velocity_y - self.bird_height) if self.bird_y < 0: self.bird_y = 0 # Update pipes' position for pipe in self.pipes: pipe["x_upper"] += self.pipe_velocity_x pipe["x_lower"] += self.pipe_velocity_x # Update pipes if 0 < self.pipes[0]["x_lower"] < 5: self.pipes.append(self.generate_pipe()) if self.pipes[0]["x_lower"] < -self.pipe_width: del self.pipes[0] if self.is_collided(): terminal = True reward = -1 self.__init__() # Draw everything if self.screen is not None: self.screen.blit(self.background_image, (0, 0)) self.screen.blit(self.base_image, (self.base_x, self.base_y)) self.screen.blit(self.bird_images[self.bird_index], (self.bird_x, self.bird_y)) for pipe in self.pipes: self.screen.blit(self.pipe_images[0], (pipe["x_upper"], pipe["y_upper"])) self.screen.blit(self.pipe_images[1], (pipe["x_lower"], pipe["y_lower"])) image = array3d(display.get_surface()) display.update() self.fps_clock.tick(self.fps) return image, reward, terminal
def someProcess(imageSurface): np_array = surfarray.array3d(imageSurface) plot_seperate_rgb(np_array.transpose((1,0,2))) surf = imageSurface #surfarray.make_surface(result) return surf
def someProcess(imageSurface): np_array = surfarray.array3d(imageSurface) plot_seperate_rgb(np_array.transpose((1, 0, 2))) surf = imageSurface #surfarray.make_surface(result) return surf
def __call__(self, imageSurface): # Convert surface to ndarray - could also directly acess pixels... np_image = surfarray.array3d(imageSurface) # Call the original function np_image_filtered = self.f(np_image) # Convert back to surface return surfarray.make_surface(np_image_filtered)
def __init__(self, competitive): self.viewer = None self.paddle1, self.paddle2, self.reward = PongObject([HALF_PAD_WIDTH - 1,HEIGHT/2], 0), PongObject([WIDTH +1 - HALF_PAD_WIDTH,HEIGHT/2], 0), [0,0] self.canvas = pygame.Surface((WIDTH, HEIGHT)) self.screen = sarray.array3d(self.canvas) self.rng = np.random.RandomState() self.action_space = spaces.Box(low=-2,high=2, shape=(2,)) self.observation_space = spaces.Box(low=0, high=255, shape=(WIDTH, HEIGHT, 3)) self.scores = [0,0] self.competitive = competitive
def reset(self): self.player = Player((WINDOW_WIDTH / 2, 3 * WINDOW_HEIGHT / 4)) self.enemies = [ Enemy( (WINDOW_WIDTH / 3, WINDOW_HEIGHT / 3), random_seed=self.random_state.randint(np.iinfo(np.uint32).max), g_cycle=self.g_cycle, r_cycle=self.r_cycle, ), Enemy( (2 * WINDOW_WIDTH / 3, WINDOW_HEIGHT / 3), random_seed=self.random_state.randint(np.iinfo(np.uint32).max), g_cycle=self.g_cycle, r_cycle=self.r_cycle, ), ] self.bullets = [] self.surface.fill(COLORS['back']) self.player.draw(self.surface) for enemy in self.enemies: enemy.draw(self.surface) for bullet in self.bullets: bullet.draw(self.surface) self.surface_image = surfarray.array3d(self.surface) gray_surface = cv2.cvtColor(self.surface_image, cv2.COLOR_RGB2GRAY) st_frame = np.full((STATE_WIDTH, STATE_HEIGHT), fill_value=COLORS['out'][0], dtype=np.uint8) pt_x_int = int(self.player.pos[0] + 0.5) pt_y_int = int(self.player.pos[1] + 0.5) st_ltrb = (max(0, pt_x_int - STATE_WIDTH // 2), max(0, pt_y_int - STATE_HEIGHT // 2), min(pt_x_int + STATE_WIDTH // 2, WINDOW_WIDTH), min(pt_y_int + STATE_HEIGHT // 2, WINDOW_HEIGHT)) st_frame[st_ltrb[0] - pt_x_int + STATE_WIDTH // 2:st_ltrb[2] - pt_x_int + STATE_WIDTH // 2, st_ltrb[1] - pt_y_int + STATE_HEIGHT // 2:st_ltrb[3] - pt_y_int + STATE_HEIGHT // 2, ] = gray_surface[st_ltrb[0]:st_ltrb[2], st_ltrb[1]:st_ltrb[3]] st_frame = st_frame.transpose(1, 0).astype(np.float32) / 127.5 - 1. self.state = np.stack([st_frame for _ in range(STATE_N_FRAMES)], axis=0) self.reward = 0. self.collision = False self.t = 0 return self.state
def grayscale(surface, brightness=1.0): """ Grayscales the surface Returns the modified surface """ array = surfarray.array3d(surface) averages = [[(r * 0.298 + g * 0.587 + b * 0.114) for (r, g, b) in col] for col in array] array = np.array([[[avg * brightness] * 3 for avg in col] for col in averages]) return surfarray.make_surface(array)
def blit_tinted(surface, image, pos, tint, src_rect=None): from Numeric import add, minimum from pygame.surfarray import array3d, pixels3d if src_rect: image = image.subsurface(src_rect) buf = Surface(image.get_size(), SRCALPHA, 32) buf.blit(image, (0, 0)) src_rgb = array3d(image) buf_rgb = pixels3d(buf) buf_rgb[...] = minimum(255, add(tint, src_rgb)).astype('b') surface.blit(buf, pos)
def blit_tinted(surface, image, pos, tint, src_rect=None): from Numeric import array, add, minimum from pygame.surfarray import array3d, pixels3d if src_rect: image = image.subsurface(src_rect) buf = Surface(image.get_size(), SRCALPHA, 32) buf.blit(image, (0, 0)) src_rgb = array3d(image) buf_rgb = pixels3d(buf) buf_rgb[...] = minimum(255, add(tint, src_rgb)).astype('b') buf_rgb = None surface.blit(buf, pos)
def update_state(self, show=False): # Render the current state self.game._draw() # Get image in pygame and convert img = s.array3d(self.game._screen).swapaxes(0, 1) full_img = img.copy() if show else None cv2.imshow('', img) cv2.waitKey(1) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img = cv2.resize(img, (WIDTH, HEIGHT), interpolation=cv2.INTER_AREA) img = np.expand_dims(img.swapaxes(0, 1), axis=0) self._state = np.concatenate((img, self._state[1:, :, :])) return full_img
def __init__(self, image, init_x, init_y): self.lives = PLAYER_LIVES self.score = 0 self.anim = 0 self.anim_frames = 0 self.damaged = np.array(surfarray.array3d(image)) self.damaged[:, :, 1:] = 0 self.damaged = surfarray.make_surface(self.damaged) self.bullets = [ Bullet(PLAYER_BULLET_COLOR, PLAYER_BULLET_SPEED, 90) for i in range(NUM_PLAYER_BULLETS) ] super().__init__(image, PLAYER_HEALTH, PLAYER_SPEED, init_x, init_y)
def runTest(self) -> None: width, height = 800, 600 screen = pygame.display.set_mode((width * 2, height)) pygame.display.set_caption("bright pass filter (bpf24)") checker = pygame.image.load( '../Assets/background_checker.png').convert() checker = pygame.transform.smoothscale(checker, (1600, 600)) background = pygame.image.load('../Assets/I1.png').convert_alpha() background = pygame.transform.smoothscale(background, (800, 600)) # array3d bpf_surface = test_bpf24_c(array3d(background), 40) self.assertRaises(OverflowError, test_bpf24_c, array3d(background), -40) self.assertRaises(OverflowError, test_bpf24_c, array3d(background), 440) # pixel3d bpf_surface = test_bpf24_c(pixels3d(background), 40) # Test a single pixel bpf_array = array3d(bpf_surface) bck_array = array3d(background) r = bck_array[100, 100, 0] g = bck_array[100, 100, 1] b = bck_array[100, 100, 2] lum = r * 0.299 + g * 0.587 + b * 0.114 # no div by zero lum must be strictly > 0 if lum > 40: c = (lum - 40) / lum self.assertEqual(bpf_array[100, 100, 0], int(r * c)) self.assertEqual(bpf_array[100, 100, 1], int(g * c)) self.assertEqual(bpf_array[100, 100, 2], int(b * c)) display(screen, checker, background, bpf_surface)
def edgeDetectionProcess(surf): """This function takes a Pygame Surface detects edges in the image, and returns a pygame surface. """ if useScipy: imageArray1 = numpy.mean(surfarray.array3d(surf),2) # converting here to one col if scipySpline: imageArray2 = edgeDetect2(imageArray1) else: imageArray2 = edgeDetect1(imageArray1) surf = surfarray.make_surface(imageArray2) else: # use pygame transform surf = transform.laplacian(surf) return surf
def edgeDetectionProcess(surf): """This function takes a Pygame Surface detects edges in the image, and returns a pygame surface. """ if useScipy: imageArray1 = numpy.mean(surfarray.array3d(surf), 2) # converting here to one col if scipySpline: imageArray2 = edgeDetect2(imageArray1) else: imageArray2 = edgeDetect1(imageArray1) surf = surfarray.make_surface(imageArray2) else: # use pygame transform surf = transform.laplacian(surf) return surf
def tryToReadImg(f): try: import Image return asarray(Image.open(f)) except ImportError: try: from pygame.image import load from pygame.surfarray import array3d,array2d if (f[-3:].upper() in ['PPM','PGM']): i = array2d(load(f)) else: i = array3d(load(f)) if (f[-3:].upper() in ['PPM']): return i > (i.max()-i.min()) return i except ImportError: print("Can't proceed: Pil or PyGame not found") exit(2)
def __init__(self, level=0, random_seed=None, player_step_width=4, enemy_step_width=2): super(DanmakuEnv, self).__init__() pygame.init() self.surface = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT)) self.surface.fill(COLORS['back']) self.surface_image = surfarray.array3d(self.surface) self.observation_space = spaces.Box( -1., 1., (STATE_N_FRAMES, STATE_HEIGHT, STATE_WIDTH), dtype=np.float32) self.action_space = spaces.Discrete(9) self.random_state = np.random.RandomState(seed=random_seed) self.player_step_width = player_step_width self.enemy_step_width = enemy_step_width sup_level = 4 self.level = level % sup_level print('game level {} ({} mod {})'.format(self.level, level, sup_level)) self.bullet_gen_interval = 10 self.bullet_speed = 6 self.bullet_g_lines = 5 self.g_radius = 8 self.r_radius = 8 self.g_cycle = 15 self.r_cycle = 10 if self.level >= 1: self.bullet_g_lines = 10 if self.level >= 2: self.bullet_gen_interval = 6 # level 3 is easier than level 2 ? if self.level >= 3: self.bullet_g_lines = 5 self.bullet_gen_interval = 5 self.g_radius = 12 self.r_radius = 10 self.g_cycle = 3 self.r_cycle = 5
def drawPiece(self): pieceImg = image.load("sprites\piece.png") p = o.players.cur pieceArray = surfarray.array3d(pieceImg) if p.c == "r": pieceArray[:, :, 1:] = 0 elif p.c == "g": pieceArray[:, :, [0, 2]] = 0 elif p.c == "b": pieceArray[:, :, :2] = 0 elif p.c == "y": pieceArray[:, :, 2] = 0 surfarray.blit_array(pieceImg, pieceArray) bpos = n.array((p.pos[0] * 20 + 1, p.pos[1] * 20 + 1)) for r in range(len(p.curPiece.m)): for c in range(len(p.curPiece.m[0])): if p.curPiece.m[r][c] == 1: pos = n.array((r * 20, c * 20)) self.window.blit(pieceImg, bpos + pos)
def gaussianBlur(imageArray): """This function takes a pygame surface, converts it to a numpy array carries out gaussian blur, converts back then returns the pygame surface. """ # Convert to a NumPy array. # In theory this should be able to be surfarray.pixels3d fro direct access. np_array = surfarray.array3d(imageArray) # The strength of the blur. sigma = 3 # Filter the image result = ndimage.filters.gaussian_filter(np_array, sigma=(sigma, sigma, 0), order=0, mode='reflect') # Convert back to a surface. surf = surfarray.make_surface(result) return surf
def get_step(self): if p_name == "Darwin": next_state = pixels3d(self.screen) else: next_state = array3d(setup.SCREEN) reward = 0 score = self.state.get_score() position_x = self.state.last_x_position if position_x > self.max_posision_x: reward += (position_x - self.max_posision_x) * 2 self.max_posision_x = position_x else: reward = 0 reward = reward + score # time penalty #reward -= 0.1 #if self.keys[275] == 1: # reward += 1 ''' if self.keys[276] == 1: reward -= 1 elif self.keys[275] == 1: reward += 1 ''' if self.keys[275] == 1: reward += 1 else: reward -= 5 ''' if self.before_x < position_x: reward += position_x - self.before_x self.before_x = position_x ''' #if position_x < 70 and position_x != 0: # self.ml_done = True return (next_state, reward, self.ml_done, self.state.clear, self.max_posision_x, self.state.timeout, position_x)
def drawBoard(self): space = image.load("sprites\space.png") pieceImg = image.load("sprites\piece.png") pieceArray = surfarray.array3d(pieceImg) for row in o.board.matrix: for cell in row: self.window.blit(space, (cell.x * 20, cell.y * 20)) if cell.color: piece = n.array(pieceArray) if cell.color == "r": piece[:, :, 1:] = 0 elif cell.color == "g": piece[:, :, [0, 2]] = 0 elif cell.color == "b": piece[:, :, :2] = 0 elif cell.color == "y": piece[:, :, 2] = 0 surfarray.blit_array(pieceImg, piece) self.window.blit(pieceImg, (cell.x * 20 + 1, cell.y * 20 + 1))
def gaussianBlur(imageArray): """This function takes a pygame surface, converts it to a numpy array carries out gaussian blur, converts back then returns the pygame surface. """ # Convert to a NumPy array. # In theory this should be able to be surfarray.pixels3d fro direct access. np_array = surfarray.array3d(imageArray) # The strength of the blur. sigma = 3 # Filter the image result = ndimage.filters.gaussian_filter(np_array, sigma=(sigma, sigma, 0), order=0, mode='reflect' ) # Convert back to a surface. surf = surfarray.make_surface(result) return surf
def send_canvas_over_spi(canvas): global spi, array3d, pi global CANVAS_WIDTH, CANVAS_HEIGHT, SYNC_PIN global log import numpy as np log.debug('send_canvas_over_spi') leds = array3d(canvas.screen).astype('uint8') leds = leds[:CANVAS_WIDTH, :CANVAS_HEIGHT, :] #leds = np.random.uniform(0, 1, size=(16, 24, 3)) * 255 #leds = leds.astype('uint8') data = leds.flatten().tobytes() # just wait, until the sync pin is set while ((pi.read_bank_1() >> SYNC_PIN) & 1) != 1: pass (num, byte) = pi.spi_read(spi, 1) pi.spi_write(spi, data)
def capture(self, cam): surf = cam.get_image() imgt = surfarray.array3d(surf) img = np.transpose(imgt, (1, 0, 2)) # swap rows and cols return img
def main(arraytype=None): """show various surfarray effects If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed. """ if arraytype is None: if 'numpy' in surfarray.get_arraytypes(): surfarray.use_arraytype('numpy') elif 'numeric' in surfarray.get_arraytype(): surfarray.use_arraytype('numeric') else: raise ImportError('No array package is installed') else: surfarray.use_arraytype(arraytype) if surfarray.get_arraytype() == 'numpy': import numpy as N from numpy import int32, uint8, uint else: import Numeric as N from Numeric import Int32 as int32, UInt8 as uint8, UInt as uint pygame.init() print ('Using %s' % surfarray.get_arraytype().capitalize()) print ('Press the mouse button to advance image.') print ('Press the "s" key to save the current image.') def surfdemo_show(array_img, name): "displays a surface, waits for user to continue" screen = pygame.display.set_mode(array_img.shape[:2], 0, 32) surfarray.blit_array(screen, array_img) pygame.display.flip() pygame.display.set_caption(name) while 1: e = pygame.event.wait() if e.type == MOUSEBUTTONDOWN: break elif e.type == KEYDOWN and e.key == K_s: #pygame.image.save(screen, name+'.bmp') #s = pygame.Surface(screen.get_size(), 0, 32) #s = s.convert_alpha() #s.fill((0,0,0,255)) #s.blit(screen, (0,0)) #s.fill((222,0,0,50), (0,0,40,40)) #pygame.image.save_extended(s, name+'.png') #pygame.image.save(s, name+'.png') #pygame.image.save(screen, name+'_screen.png') #pygame.image.save(s, name+'.tga') pygame.image.save(screen, name+'.png') elif e.type == QUIT: raise SystemExit() #allblack allblack = N.zeros((128, 128), int32) surfdemo_show(allblack, 'allblack') #striped #the element type is required for N.zeros in NumPy else #an array of float is returned. striped = N.zeros((128, 128, 3), int32) striped[:] = (255, 0, 0) striped[:,::3] = (0, 255, 255) surfdemo_show(striped, 'striped') #imgarray imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp') imgsurface = pygame.image.load(imagename) imgarray = surfarray.array2d(imgsurface) surfdemo_show(imgarray, 'imgarray') #flipped flipped = imgarray[:,::-1] surfdemo_show(flipped, 'flipped') #scaledown scaledown = imgarray[::2,::2] surfdemo_show(scaledown, 'scaledown') #scaleup #the element type is required for N.zeros in NumPy else #an #array of floats is returned. size = N.array(imgarray.shape)*2 scaleup = N.zeros(size, int32) scaleup[::2,::2] = imgarray scaleup[1::2,::2] = imgarray scaleup[:,1::2] = scaleup[:,::2] surfdemo_show(scaleup, 'scaleup') #redimg rgbarray = surfarray.array3d(imgsurface) redimg = N.array(rgbarray) redimg[:,:,1:] = 0 surfdemo_show(redimg, 'redimg') #soften #having factor as an array forces integer upgrade during multiplication #of rgbarray, even for numpy. factor = N.array((8,), int32) soften = N.array(rgbarray, int32) soften[1:,:] += rgbarray[:-1,:] * factor soften[:-1,:] += rgbarray[1:,:] * factor soften[:,1:] += rgbarray[:,:-1] * factor soften[:,:-1] += rgbarray[:,1:] * factor soften //= 33 surfdemo_show(soften, 'soften') #crossfade (50%) src = N.array(rgbarray) dest = N.zeros(rgbarray.shape) dest[:] = 20, 50, 100 diff = (dest - src) * 0.50 xfade = src + diff.astype(uint) surfdemo_show(xfade, 'xfade') #alldone pygame.quit()
def main(arraytype=None): """show various surfarray effects If arraytype is provided then use that array package. Valid values are 'numeric' or 'numpy'. Otherwise default to NumPy, or fall back on Numeric if NumPy is not installed. """ if arraytype not in ('numpy', None): raise ValueError('Array type not supported: %r' % arraytype) import numpy as N from numpy import int32, uint8, uint pygame.init() print ('Using %s' % surfarray.get_arraytype().capitalize()) print ('Press the mouse button to advance image.') print ('Press the "s" key to save the current image.') #allblack allblack = N.zeros((128, 128), int32) surfdemo_show(allblack, 'allblack') #striped #the element type is required for N.zeros in NumPy else #an array of float is returned. striped = N.zeros((128, 128, 3), int32) striped[:] = (255, 0, 0) striped[:,::3] = (0, 255, 255) surfdemo_show(striped, 'striped') #rgbarray imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp') imgsurface = pygame.image.load(imagename) rgbarray = surfarray.array3d(imgsurface) surfdemo_show(rgbarray, 'rgbarray') #flipped flipped = rgbarray[:,::-1] surfdemo_show(flipped, 'flipped') #scaledown scaledown = rgbarray[::2,::2] surfdemo_show(scaledown, 'scaledown') #scaleup #the element type is required for N.zeros in NumPy else #an #array of floats is returned. shape = rgbarray.shape scaleup = N.zeros((shape[0]*2, shape[1]*2, shape[2]), int32) scaleup[::2,::2,:] = rgbarray scaleup[1::2,::2,:] = rgbarray scaleup[:,1::2] = scaleup[:,::2] surfdemo_show(scaleup, 'scaleup') #redimg redimg = N.array(rgbarray) redimg[:,:,1:] = 0 surfdemo_show(redimg, 'redimg') #soften #having factor as an array forces integer upgrade during multiplication #of rgbarray, even for numpy. factor = N.array((8,), int32) soften = N.array(rgbarray, int32) soften[1:,:] += rgbarray[:-1,:] * factor soften[:-1,:] += rgbarray[1:,:] * factor soften[:,1:] += rgbarray[:,:-1] * factor soften[:,:-1] += rgbarray[:,1:] * factor soften //= 33 surfdemo_show(soften, 'soften') #crossfade (50%) src = N.array(rgbarray) dest = N.zeros(rgbarray.shape) # dest is float64 by default. dest[:] = 20, 50, 100 diff = (dest - src) * 0.50 xfade = src + diff.astype(uint) surfdemo_show(xfade, 'xfade') #alldone pygame.quit()
#SET IMAGE DEFORMATION (Min: 2) deform = 10 #SET UPDATE TIMER timer = 1000 #START FILLED? filled = True #SLOWLY BETTER PICTURE? (Deform Min: 10) better = True imgarray = surfarray.array3d(imgsurface) blue = (0,0,255) white = (255,255,255) pink = (255,200,200) Width = imgsurface.get_width() Height = imgsurface.get_height() screen = pygame.display.set_mode((Width,Height)) back = pygame.Surface((Width,Height)) background = back.convert() background.fill(white) screen.blit(background,(0,0))