def _new_child(self,id_val,image,sort_add=False): self.Hide() try: if self.child_kwargs is None: kws = {} else: kws = self.child_kwargs child = PointDisplayCanvas(self,-1,**kws) finally: self.Show() child.set_fullcanvas(True) pygim = ArrayInterfaceImage( image, allow_copy=False ) child.new_image( pygim ) child.set_rotate_180( self.rotate_180 ) child.set_flip_LR( self.flip_lr ) self.children[id_val] = child self.lbrt[id_val] = () id_vals = self.children.keys() id_vals.sort() if sort_add: # maintain ordering self.box = wx.BoxSizer(wx.HORIZONTAL) self.SetSizer(self.box) for id_val in id_vals: child = self.children[id_val] self.box.Add( child, 1, wx.EXPAND|wx.ALL, border=1) else: self.box.Add( child, 1, wx.EXPAND|wx.ALL, border=1) self.Layout()
def __init__(self, height=84, width=84, resize_ratio=1): from pyglet.gl import glEnable, glBlendFunc from pyglet.gl import GL_BLEND, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA from pyglet import window, image from pygarrayimage.arrayimage import ArrayInterfaceImage self.width = width self.height = height self.resize_ratio = resize_ratio self.window = window.Window(width=int(width * resize_ratio), height=int(height * resize_ratio), visible=False, resizable=True) self.arr = np.zeros( (int(height * resize_ratio), int(width * resize_ratio)), dtype=np.uint8) self.aii = ArrayInterfaceImage(self.arr) self.img = self.aii.texture checks = image.create(32, 32, image.CheckerImagePattern()) self.background = image.TileableTexture.create_for_image(checks) self.window.set_visible() glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
def __init__(self): super().__init__() window = system.get_window() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) base_path = os.path.join('images', 'space_background_pack', 'layers') self.background = load(os.path.join(base_path, 'parallax-space-background.png')) self.background_sprite = Sprite(self.background) big_planet = load(os.path.join(base_path, 'parallax-space-big-planet.png')) self.big_planet_sprite = Sprite(big_planet) far_planet = load(os.path.join(base_path, 'parallax-space-far-planets.png')) self.far_planet_sprite = Sprite(far_planet) ring_planet = load(os.path.join(base_path, 'parallax-space-ring-planet.png')) self.ring_planet_sprite = Sprite(ring_planet) self.ring_planet_sprite.x = 100 self.ring_planet_sprite.y = 100 stars = load(os.path.join(base_path, 'parallax-space-stars.png')) self.stars_sprite = Sprite(stars) self.scheduled_functions = ( (self.randomize_projectiles, 2), (self.check_direction, 1 / 120), (self.generate_powerup, 20) ) if config.get_config('control') == 1: self.face_cascade = cv.CascadeClassifier( 'venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') self.facecam = Facecam(face_cascade=self.face_cascade).start() self.aii = ArrayInterfaceImage(self.facecam.read(), 'BGR') self.image = self.aii.texture @self.facecam.event('on_face_move') def follow_face(x, y, width, height): # Need to flip things first to work x = self.image.width - x y = self.image.height - y new_x = self.border.x + x - width // 2 new_y = self.border.y + y - height // 2 self.player.move(new_x, new_y) self.scheduled_functions = ( (self.randomize_projectiles, 2), (self.generate_powerup, 20), ) window.push_handlers(self.facecam) self.player = Player(src='images/ufo.png') self.border = Rectangle( width=(500 if not self.image else self.image.width) + 10, height=(500 if not self.image else self.image.height) + 10, color=(0, 0, 0, 127) ) self.player.scale = 1.2 self.batch = Batch() self.projectiles: List[Projectile] = [] window.push_handlers(self.keys) def pause_game(symbol, modifiers): if symbol == key.ESCAPE: self.enemy.running = not self.enemy.running self.enemy.attack_pattern.running = not self.enemy.attack_pattern.running self.player.running = not self.player.running window.on_key_press = pause_game for func, interval in self.scheduled_functions: clock.schedule_interval(func, interval) self.fps_display = FPSDisplay(window) self.generate_enemy(0) self.resize()
width, height= 320,240 depth=4 # 1, 3, or 4 arr = numpy.arange( width*height*depth, dtype=numpy.uint8) arr.shape = height,width,depth if depth==1 and 1: # test 2D array arr.shape = height,width else: # test constructor from PIL image import Image if Image.VERSION < '1.1.6': # new PIL necessary for __array_interface__ raise ValueError("Need Image (PIL) version 1.1.6 ") arr = Image.open(filename) #arr = numpy.asarray( pil_image ) aii = ArrayInterfaceImage(arr) img = aii.texture checks = image.create(32, 32, image.CheckerImagePattern()) background = image.TileableTexture.create_for_image(checks) w.width = img.width w.height = img.height w.set_visible() glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) i=0 while not w.has_exit:
def play(frames, delay=0, magnify=1, background=None, blit_position=None, show_frame_number=True): w = window.Window(visible=False, resizable=True) arr = numpy.zeros([100, 100], dtype=numpy.uint8) aii = ArrayInterfaceImage(arr) img = aii.texture if background is None: checks = image.create(32, 32, image.CheckerImagePattern()) background = image.TileableTexture.create_for_image(checks) background_tiled = True else: if background.dtype != np.uint8: 'converting background to uint8' background = np.array(background, dtype=np.uint8) * 255 background_aii = ArrayInterfaceImage(background) background = background_aii.texture background_tiled = False if background_tiled: w.width = img.width w.height = img.height w.set_visible() else: w.width = background.width w.height = background.height w.set_visible() LIBGL_ALWAYS_SOFTWARE = 1 glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for i, f in enumerate(frames): arr = f if arr is not None: if arr.dtype != np.uint8: 'converting' arr = np.array(arr, dtype=np.uint8) * 255 if magnify != 1: newshape = [arr.shape[0] * magnify, arr.shape[1] * magnify] arr = nim.rebin(arr, newshape) try: aii.view_new_array(arr) except: # size changed print 'size changed!' #w.width = arr.shape[1] #w.height = arr.shape[0] aii = ArrayInterfaceImage(arr) img = aii.texture w.dispatch_events() w.clear() if background_tiled: background.blit_tiled(0, 0, 0, 100, 100) #w.width, w.height) else: background.blit(0, 0, 0) # add some overlays: if show_frame_number: s = 'frame: ' + str(i) label = pyglet.text.Label(s, font_name='courier', font_size=36, x=10, y=10) label.draw() if blit_position is None: img.blit(0, 0, 0) else: img.blit(blit_position[i][1], blit_position[i][0], 0) #frame_number_text.draw() if 0: r = arr.shape[0] / 2. body_axis = npmovie.kalmanobj.long_axis[f] wing_axis = npmovie.kalmanobj.wingaxisR[f] wing_center = npmovie.kalmanobj.wingcenterR[f] #print wing_center, wing_axis if mode == 'wingimgR': print f, uframe.wingimgR.sum() pyglet.graphics.draw( 2, pyglet.gl.GL_LINES, ('v2i', (int(r), int(r), int(body_axis[1] * 10 * magnify) + int(r), int(body_axis[0] * 10 * magnify) + int(r)))) try: pyglet.graphics.draw( 2, pyglet.gl.GL_LINES, ('v2i', (int(r), int(r), int(wing_center[1] * magnify), int(wing_center[0] * magnify)))) pyglet.graphics.draw(1, pyglet.gl.GL_POINTS, ('v2i', (int(wing_center[1] * magnify), int(wing_center[0] * magnify)))) pass except: pass w.flip() time.sleep(delay) # slow down the playback w.close()
from pyglet import gl from pyglet.window import FPSDisplay from pygarrayimage.arrayimage import ArrayInterfaceImage gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) face_cascade = cv2.CascadeClassifier( 'venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') # facecam = Facecam(face_cascade, src='ACM App.mp4').start() img = cv2.imread('faces/four.jpeg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) aii = ArrayInterfaceImage(img, 'BGR') soul = pyglet.image.load('chihiro.png') soul_sprite = pyglet.sprite.Sprite(soul, x=0, y=0) image = aii.texture window = pyglet.window.Window(vsync=False) window.maximize() # window.height = image.height # window.width = image.width fps_display = FPSDisplay(window) box = Rectangle(x=0, y=0, width=100, height=100) @window.event def on_draw(): window.clear() image.blit(0, 0, 0)
def __init__(self, **win_args): self.array = win_args.pop('array') self.aii = ArrayInterfaceImage(self.array) super(WindowArray, self).__init__(**win_args)