def __init__(self, _w, _h, _scale): self.height = _h self.width = _w self.scale = _scale white_pixel = make_surface(np.ones((self.scale, self.scale), dtype=int)*255) black_pixel = make_surface(np.zeros((self.scale, self.scale), dtype=int)) self.surface_pixels = {0: black_pixel, 1: white_pixel} self.pixels_matrix = None self.to_redraw = None self.clear()
def addImage(self, im): surf = surfarray.make_surface(im.raw_data[:, :, :3]) thumbnail = pygame.transform.scale(surf, (38, 38)) self.imageList.append(thumbnail) button_offset = np.add(self.pos, (56, 6 + self.id * 50)) rect = pygame.Rect(button_offset, (38, 38)) button_confirm = PygButton(rect, bgcolor=[80, 80, 80], fgcolor=[30, 30, 30], image_path="resources/icons/confirm.png") def confirm_callback(event): id = self.confirm_buttonList.index(button_confirm) self.selectImage(id) button_confirm.mouseClickCallback = confirm_callback self.confirm_buttonList.append(button_confirm) button_offset = np.add(self.pos, (106, 6 + self.id * 50)) rect = pygame.Rect(button_offset, (38, 38)) button_cancel = PygButton(rect, bgcolor=[80, 80, 80], fgcolor=[30, 30, 30], image_path="resources/icons/cancel.png") def cancel_callback(event): id = self.cancel_buttonList.index(button_cancel) self.deleteImage(id) button_cancel.mouseClickCallback = cancel_callback self.cancel_buttonList.append(button_cancel) self.focus = self.id self.id += 1
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 whiteStar(r): a = np.full((3, 3, 3), 0, dtype=int) a[1][1] = (255, 255, 255) # a[1][2] = (255, 255, 255) # a[2][1] = (255, 255, 255) # a[2][2] = (255, 255, 255) return tf.smoothscale(sfa.make_surface(a), (r, r))
def generate(self): random_image = np.random.uniform(self.min, self.max, (self.size, self.size, 3)) random_image = random_image.astype('int') surf = surfarray.make_surface(random_image) return surf
def generate(self): """ Generate a pyame Surface with pixels following a circular striped pattern from the center of the parent entity :param width: the width of the generated surface :param height: the height of the generated surface :return: the pygame Surface """ img = np.zeros((self.size, self.size, 3)) colors = random.choices(self.colors, k=self.n_stripes) x = (self.size - 1) / 2 y = (self.size - 1) / 2 for i in range(self.size): for j in range(self.size): angle = int( np.arctan2(j - y, i - x) / (2 * math.pi / self.n_stripes)) img[i, j, :] = colors[angle] surf = surfarray.make_surface(img) return surf
def generate(self): size_shrink = (int(self.size*1.0/self.size_tiles), int(self.size*1.0/self.size_tiles), 3) random_image = self.rng_texture.uniform(self.min, self.max, size_shrink).astype('int') random_image = resize(random_image, (self.size, self.size), order=0, preserve_range=True) surf = surfarray.make_surface(random_image) return surf
def generate(self): super().generate() assert self._size_tiles <= self._size[ 0] and self._size_tiles <= self._size[1] radius = max(self._size) n_stripes = int(2 * math.pi * radius / self._size_tiles) img = np.zeros((*self._size, 3)) colors = [[ self._rng.integers(self._min[i], self._max[i], endpoint=True) for i in range(3) ] for _ in range(n_stripes)] x = (self._size[0] - 1) / 2 y = (self._size[1] - 1) / 2 for i in range(self._size[0]): for j in range(self._size[1]): angle = int( np.arctan2(j - y, i - x) / (2 * math.pi / n_stripes)) img[i, j, :] = colors[angle] surf = surfarray.make_surface(img) self._surface = surf return self._surface
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 generate(self): """ Generate a pyame Surface with pixels following a circular striped pattern from the center of the parent entity :param width: the width of the generated surface :param height: the height of the generated surface :return: the pygame Surface """ super().generate() radius = max(self._size) n_stripes = int(2 * math.pi * radius / self._size_tiles) img = np.zeros((*self._size, 3)) colors = self._rng.choice(self._colors, size=n_stripes, replace=True) x = (self._size[0] - 1) / 2 y = (self._size[1] - 1) / 2 for i in range(self._size[0]): for j in range(self._size[1]): angle = int( np.arctan2(j - y, i - x) / (2 * math.pi / n_stripes)) img[i, j, :] = colors[angle] surf = surfarray.make_surface(img) self._surface = surf return self._surface
def load_images(self): files = remove_DS_store(os.listdir(self.unannotated_img_path)) self.images = { 'img': [], 'img_name': [], 'img_dims': [], 'img_resized_dims': [] } files.sort() for file in files: print(file) img = cv2.imread(self.unannotated_img_path + file) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.images['img_name'].append(file) self.images['img_dims'].append((img.shape[0], img.shape[1])) # img = cv2.resize(img,(self.display_width-self.ui_margin,self.display_height)) img = self.image_resize(img, height=int(self.display_height)) self.images['img_resized_dims'].append( (img.shape[0], img.shape[1])) img = cv2.flip(img, 0) img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) img = make_surface(img) self.images['img'].append(img)
def generate(self): """ Generate a pygame Surface with pixels following a circular striped pattern from the center of the parent entity :param width: the width of the generated surface :param height: the height of the generated surface :return: the pygame Surface """ img = np.zeros( (self.size, self.size , 3) ) x = (self.size - 1) / 2 y = (self.size - 1) / 2 for i in range(self.size): for j in range(self.size): angle = np.arctan2( j - y, i - x) % (2*math.pi/self.n_stripes) if angle > math.pi/(self.n_stripes) : img[i, j, :] = self.color_1 else: img[i, j, :] = self.color_2 surf = surfarray.make_surface(img) return surf
def generate(self): """ Generate a pygame Surface with pixels following a circular striped pattern from the center of the parent entity :param width: the width of the generated surface :param height: the height of the generated surface :return: the pygame Surface """ super().generate() img = np.zeros((*self._size, 3)) x = (self._size[0] - 1) / 2 y = (self._size[1] - 1) / 2 for i in range(self._size[0]): for j in range(self._size[1]): angle = (np.arctan2(j - y, i - x) - math.pi / self.n_stripes / 2.) % (2 * math.pi / self.n_stripes) if angle > math.pi / self.n_stripes: img[i, j, :] = self._color_1 else: img[i, j, :] = self._color_2 self._surface = surfarray.make_surface(img) return self._surface
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 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 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): frame = self.capture() c_index = 0 for group in self.screen.positions: for pos in self.screen.positions[group]: x, y = pos x = int(x + 0.5) y = int(y + 0.5) start = (x - 3, y - 3) end = (x + 3, y + 3) cv2.rectangle(frame, start, end, self.colors[c_index], 2) c_index += 1 c_index %= len(self.colors) x, y = self.position start = (int(x + 0.5) - 10, int(y + 0.5) - 10) end = (int(x + 0.5) + 10, int(y + 0.5) + 10) cv2.rectangle(frame, start, end, (0xFF, 0xFF, 0), 1) width, height, channel = frame.shape frame = np.swapaxes(frame, 0, 1) n_frame = frame.copy() n_frame[:, :, 0] = frame[:, :, 2] n_frame[:, :, 2] = frame[:, :, 0] frame = cv2.resize(n_frame, None, fx=self.ratio, fy=self.ratio, interpolation=cv2.INTER_CUBIC) frame = np.flip(frame, 0) camera = surfarray.make_surface(frame) self.surface.blit(camera, (0, 0))
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 change_color(self): texture_cv2 = cv2.imread('textures/car1_a.png', cv2.IMREAD_UNCHANGED) texture_cv2 = cv2.cvtColor(texture_cv2, cv2.COLOR_BGR2RGB) texture_cv2[(texture_cv2[:, :, 0] == 0) & \ (texture_cv2[:, :, 1] == 255) & \ (texture_cv2[:, :, 2] == 0)] = self.__color self.__texture = surfarray.make_surface(texture_cv2) self.__texture.set_colorkey((255,) * 3)
def generate(self): super().generate() random_image = self._rng.uniform(self._min, self._max, (*self._size, 3)) random_image = random_image.astype('int') surf = surfarray.make_surface(random_image) self._surface = surf return self._surface
def gol_show_grid_evolution(screen, delay=0): '''The GoL grid is displayed by this function.''' su=sa.make_surface(a[1:N,1:N]) su_new=pg.transform.scale(su, (disp_N, disp_N)) screen.blit(su_new,(0,0)) pg.display.update() if delay != 0: sleep(delay)
def np_to_img(np_array): img = 255 * np_array.T / np_array.max() # print(img) w, h = img.shape ret = np.empty((w, h, 3), dtype=np.uint8) ret[:, :, 2] = ret[:, :, 1] = ret[:, :, 0] = img surf = surfarray.make_surface(ret) return surf
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 __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 _smooth(surface): w,h = surface.get_size() arrayrgb = surfarray.pixels3d(surface) sum_array = numpy.zeros(arrayrgb.shape) for d in mapgen.MOORE: sum_array += numpy.roll( numpy.roll(arrayrgb[:,:], d[0], axis=0), d[1], axis=1 ) sum_array /= len(mapgen.MOORE) s = surfarray.make_surface(sum_array) return s
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 __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 generate(self): """ Generate a pygame Surface with pixels following a uniform density :param width: the width of the generated Surface :param height: the height of the generated Surface :return: the pygame Surface """ color = self.list_rgb_colors.pop() min_color = [ max(0, x - self.delta_uniform) for x in color] max_color = [ min(255, x + self.delta_uniform) for x in color] random_image = self.rng_texture.uniform(min_color, max_color, (int(self.size*1.0/self.size_tiles), int(self.size*1.0/self.size_tiles), 3)).astype('int') random_image = resize(random_image, (self.size, self.size), order=0, preserve_range=True) surf = surfarray.make_surface(random_image) return surf
def Refresh(self): clock = pygame.time.Clock() self.move_Win=False #移动窗口缓存 pos_Cache=Point2D() #移动窗口缓存 while True: clock.tick(30) #频率稳定器 self.window.fill((100,100,100)) self.canvas.blit(make_surface(self.parser.map),(0,0)) self.back.blit(self.canvas,(0,0)) self.window.blit(self.back,(0,100)) odom_txt="Odometry-X:%f\n Y:%f\n Angular %f"%(self.parser.robot_Odometry[0],self.parser.robot_Odometry[1],self.parser.robot_Odometry[2]) odom_surf=self.Render_Text(odom_txt) self.window.blit(odom_surf,(0,0)) pygame.display.update() #更新界面 self.Keyboard() self.parser.Manual(v=self.v,vth=self.vth)
def get_image(self,surface=None,*argv,**argd): """ Since we are using opencv there are some differences here. This function will return a cvmat by default (the quickest route from camera to your code) Or if pygame was specified a 3dpixel array - this can be blitted directly to a pygame surface - or can be converted to a surface array and returned to your pygame code. """ if self.imageType == "pygame": try: surfarray.blit_array(surface,conversionUtils.cv2SurfArray(hg.cvQueryFrame(self.capture))) return surface except: return surfarray.make_surface(conversionUtils.cv2SurfArray(hg.cvQueryFrame(self.capture))).convert() return hg.cvQueryFrame(self.capture)
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 generate(self): super().generate() shape_mini = (max(1, int(self._size[0] * 1.0 / self._size_tiles)), max(1, int(self._size[1] * 1.0 / self._size_tiles)), 3) random_image = self._rng.uniform(self._min, self._max, shape_mini).astype('int') random_image = resize(random_image, self._size, order=0, preserve_range=True) surf = surfarray.make_surface(random_image) self._surface = surf return self._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 getCamFrame(self): ''' 摄像头获取一帧数据 :param camera: 摄像头 :return: ''' # 清空人脸方框数据 self.faceRects.clear() if not self.camera.isOpened(): print('摄像头没有打开') return None # 获取图像 retval, frame = self.camera.read() if retval: print('成功-------------------------------------------------------') frame = cvtColor(frame, COLOR_BGR2RGB) [h, w] = frame.shape[:2] # tensorflow检测人脸 (boxes, scores, classes, num_detections) = self.tfDetector.run(frame) boxes = squeeze(boxes) scores = squeeze(scores) for i in range(0, self.faceCount): if scores[i] > 0.3: ymin, xmax, ymax, xmin = boxes[i, 0], boxes[i, 1], boxes[ i, 2], boxes[i, 3] left, right, top, bottom = (xmin * w, xmax * w, ymin * h, ymax * h) # 处理左右坐标 left = WINDOW_WIDTH - left right = WINDOW_WIDTH - right self.faceRects.append((left, right, top, bottom)) print('--------left', left, ' right', right) # cv2.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (0, 255, 255), 2) frame = rot90(frame) frame = surfarray.make_surface(frame) return frame else: print('失败-------------------------------------------------------') return None
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
raw_input("Press Enter to pass to cvEyeTrack to add a cross") green = opencv.CV_RGB(0,255,0) Draw_Cross(ipl,200,200,50,100,green) print("Added a cross") raw_input("Press Enter to pass to back from opencv to pygame") npy2 = adaptors.Ipl2NumPy(ipl) npy2.transpose(1,0,2) """ In [2]: type(npy2) Out[2]: <type 'numpy.ndarray'> In [3]: npy2.shape Out[3]: (640, 480, 3) """ snapshot2 = surfarray.make_surface(npy2) print("Converted.") raw_input("Press Enter to display the new image") display.blit(snapshot2,(0,0)) pygame.display.flip() pygame.display.flip() raw_input("Press Enter to exit")
for frame in range(skipFrame): gamescreen = ale.getScreenGrayscale(gamescreen) # convert ALE gamescreen into usable image, scaled between 0 and 1 processedImg = gamescreen[33:-16, :, 0] frames.append(processedImg) reward += ale.act(a) total_reward += reward currrewards.append(reward) currstates.append(imresize(np.asarray(frames), 0.525, interp='nearest')) frames = np.swapaxes(np.asarray(frames),0,2) from pygame import surfarray frames = surfarray.make_surface(frames) screen.blit(pygame.transform.scale(frames, (screen_width*2, screen_height*2)),(0,0)) #get RAM ram_size = ale.getRAMSize() ram = np.zeros((ram_size),dtype=np.uint8) ale.getRAM(ram) #Display ram bytes font = pygame.font.SysFont("Ubuntu Mono",32) text = font.render("RAM: " ,1,(255,208,208)) screen.blit(text,(330,10)) font = pygame.font.SysFont("Ubuntu Mono",25) height = font.get_height()*1.2