class Viewer(object): 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 render(self, new_arr): assert new_arr.shape == ( self.height, self.width ), "the array shape {0} should be consistent with stipulated shape {1}!".format( new_arr.shape, (self.height, self.width)) if self.resize_ratio != 1: new_arr = np.repeat(np.repeat(new_arr, int(self.resize_ratio), 0), int(self.resize_ratio), 1) self.window.dispatch_events() self.background.blit_tiled(0, 0, 0, self.window.width, self.window.height) self.img.blit(0, 0, 0) self.window.flip() np.place(self.arr, self.arr >= -1, new_arr.astype('uint8')[::-1, :]) self.aii.dirty( ) # dirty the ArrayInterfaceImage because the data changed def close(self): self.window.close()
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: w.dispatch_events() background.blit_tiled(0, 0, 0, w.width, w.height) img.blit(0, 0, 0) w.flip() if filename is None and 1: # modify numpy array in-place arr.fill(i) aii.dirty() # dirty the ArrayInterfaceImage because the data changed i=(i+1)%256 if i == 1 and 0: arr = numpy.ones_like( arr ) # create a new array aii.view_new_array(arr) # switch ArrayInterfaceImage to view the new array
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: w.dispatch_events() background.blit_tiled(0, 0, 0, w.width, w.height) img.blit(0, 0, 0) w.flip() if filename is None and 1: # modify numpy array in-place arr.fill(i) aii.dirty() # dirty the ArrayInterfaceImage because the data changed i = (i + 1) % 256 if i == 1 and 0: arr = numpy.ones_like(arr) # create a new array aii.view_new_array(arr) # switch ArrayInterfaceImage to view the new array