def reset(self): self.entry_pressure = np.min(self.pressures) if self.conditional: self.random_target = self.get_random_target(num=1, squeeze=True) else: self.random_target = None self.s = tiledsurface.Surface() self.s.flood_fill(0, 0, (255, 255, 255), (0, 0, 64, 64), 0, self.s) self.s.begin_atomic() with open(self.args.brush_path) as fp: self.bi = brush.BrushInfo(fp.read()) self.b = brush.Brush(self.bi) self._step = 0 self.s_x, self.s_y = None, None if self.args.conditional: self.z = None else: self.z = np.random.uniform(-0.5, 0.5, size=self.args.z_dim) return self.state, self.random_target, self.z
def saveFrame(): print('test-saving various frame sizes...') cnt = 0 doc = document.Document() #doc.load('bigimage.ora') doc.set_frame_enabled(True) s = tiledsurface.Surface() N = mypaintlib.TILE_SIZE positions = range(-1, +2) + range(-N - 1, -N + 2) + range(+N - 1, +N + 2) for x1 in positions: for x2 in positions: for y1 in positions: for y2 in positions: if x2 <= x1 or y2 <= y1: continue cnt += 1 x, y, w, h = x1, y1, x2 - x1, y2 - y1 #print x, y, w, h s.save_as_png('test_saveFrame_s.png', x, y, w, h) doc.update_frame(x=x, y=y, width=w, height=h) #doc.save('test_saveFrame_doc_%dx%d.png' % (w,h)) doc.save('test_saveFrame_doc.png') doc.save('test_saveFrame_doc.jpg') print('checked', cnt, 'different rectangles')
def test_save_doc_in_frame(self): """Test saving many different crops of bigimage.ora""" cnt = 0 doc = document.Document() doc.load(join(paths.TESTS_DIR, 'bigimage.ora')) doc.set_frame_enabled(True) s = tiledsurface.Surface() t0 = time() positions = list(range(-1, +2)) positions.extend(range(-N - 1, -N + 2)) positions.extend(range(+N - 1, +N + 2)) for x1 in positions: for x2 in positions: for y1 in positions: for y2 in positions: if x2 <= x1 or y2 <= y1: continue cnt += 1 x, y, w, h = x1, y1, x2 - x1, y2 - y1 # print x, y, w, h s.save_as_png('test_saveFrame_s.png', x, y, w, h) doc.update_frame(x=x, y=y, width=w, height=h) # doc.save('test_saveFrame_doc_%dx%d.png' % (w,h)) doc.save('test_saveFrame_doc.png') doc.save('test_saveFrame_doc.jpg') print( "saved %d frames in %0.2fs, " % (cnt, time() - t0), end="", file=sys.stderr, )
def test_brush_paint(self): """30s of painting at 4x with a charcoal brush""" s = tiledsurface.Surface() myb_path = join(paths.TESTS_DIR, 'brushes/v2/charcoal.myb') with open(myb_path, "r") as fp: bi = brush.BrushInfo(fp.read()) b = brush.Brush(bi) events = np.loadtxt(join(paths.TESTS_DIR, 'painting30sec.dat')) bi.set_color_rgb((0.0, 0.9, 1.0)) t0 = time() for i in range(10): t_old = events[0][0] for t, x, y, pressure in events: dtime = t - t_old t_old = t s.begin_atomic() b.stroke_to( s.backend, x * 4, y * 4, pressure, 0.0, 0.0, dtime, 1.0, # view zoom 0.0, # view rotation ) s.end_atomic() print('%0.4fs, ' % (time() - t0,), end="", file=sys.stderr) # FIXME: why is this time so different each run? # print(s.get_bbox(), b.get_total_stroke_painting_time()) s.save_as_png('test_brushPaint.png')
def brushengine_paint_hires(): from lib import tiledsurface, brush s = tiledsurface.Surface() with open('brushes/watercolor.myb') as fp: bi = brush.BrushInfo(fp.read()) b = brush.Brush(bi) events = np.loadtxt('painting30sec.dat') t_old = events[0][0] yield start_measurement s.begin_atomic() trans_time = 0.0 for t, x, y, pressure in events: dtime = t - t_old t_old = t b.stroke_to(s.backend, x * 5, y * 5, pressure, 0.0, 0.0, dtime) trans_time += dtime if trans_time > 0.05: trans_time = 0.0 s.end_atomic() s.begin_atomic() s.end_atomic() yield stop_measurement
def brushPaint(): s = tiledsurface.Surface() with open('brushes/charcoal.myb') as fp: bi = brush.BrushInfo(fp.read()) b = brush.Brush(bi) events = np.loadtxt('painting30sec.dat') bi.set_color_rgb((0.0, 0.9, 1.0)) t0 = time() for i in range(10): t_old = events[0][0] for t, x, y, pressure in events: dtime = t - t_old t_old = t s.begin_atomic() b.stroke_to(s.backend, x * 4, y * 4, pressure, 0.0, 0.0, dtime) s.end_atomic() print('Brushpaint time:', time() - t0) # FIXME: why is this time so different each run? print(s.get_bbox(), b.get_total_stroke_painting_time()) s.save_as_png('test_brushPaint.png')
def test_save_doc_in_frame(self): """Test saving many different crops of bigimage.ora""" cnt = 0 doc = document.Document() doc.load(join(paths.TESTS_DIR, 'bigimage.ora')) doc.set_frame_enabled(True) s = tiledsurface.Surface() t0 = time() positions = list(range(-1, +2)) positions.extend(range(-N - 1, -N + 2)) positions.extend(range(+N - 1, +N + 2)) def valid(c): x1, y1, x2, y2 = c return x1 < x2 and y1 < y2 for x1, y1, x2, y2 in filter(valid, product(positions, repeat=4)): cnt += 1 x, y, w, h = x1, y1, x2 - x1, y2 - y1 s.save_as_png('test_saveFrame_s.png', x, y, w, h) doc.update_frame(x=x, y=y, width=w, height=h) doc.save('test_saveFrame_doc.png') doc.save('test_saveFrame_doc.jpg') print( "saved %d frames in %0.2fs, " % (cnt, time() - t0), end="", file=sys.stderr, )
def reset(self): if self.conditional: self.random_target = self.get_random_target() else: self.random_target = None self.s = tiledsurface.Surface() self.s.flood_fill(0, 0, (255, 255, 255), (0, 0, 64, 64), 0, self.s) self._step = 0 return self.state, self.random_target
def directPaint(): s = tiledsurface.Surface() events = np.loadtxt('painting30sec.dat') s.begin_atomic() for t, x, y, pressure in events: r = g = b = 0.5 * (1.0 + np.sin(t)) r *= 0.8 s.draw_dab(x, y, 12, r, g, b, pressure, 0.6) s.end_atomic() s.save_as_png('test_directPaint.png')
def reset(self): if self.conditional: self.random_target = self.get_random_target(num=1, squeeze=True) else: self.random_target = None self.s = tiledsurface.Surface() self.s.flood_fill(0, 0, (255, 255, 255), (0, 0, 64, 64), 0, self.s) self.s.begin_atomic() self._step = 0 self.s_x, self.s_y = None, None return self.state, self.random_target
def test_direct_paint(self): """30s of painting at 1x with the default brush""" s = tiledsurface.Surface() events = np.loadtxt(join(paths.TESTS_DIR, 'painting30sec.dat')) t0 = time() s.begin_atomic() for t, x, y, pressure in events: r = g = b = 0.5 * (1.0 + np.sin(t)) r *= 0.8 s.draw_dab(x, y, 12, r, g, b, pressure, 0.6) s.end_atomic() s.save_as_png('test_directPaint.png') print('%0.4fs, ' % (time() - t0, ), end="", file=sys.stderr)
def reset(self): self.intermediate_images = [] self.prev_x, self.prev_y, self.prev_pressure = None, None, None self.s = tiledsurface.Surface() self.s.flood_fill(0, 0, (255, 255, 255), (0, 0, 64, 64), 0, self.s) self.s.begin_atomic() with open(self.brush_path) as fp: self.bi = brush.BrushInfo(fp.read()) self.b = brush.Brush(self.bi) # Two extra brushstrokes for MyPaint 2 self.b.stroke_to(self.s.backend, 20, 20, 0.0, 0.0, 0.0, 0.1, 0, 0, 0) self.b.stroke_to(self.s.backend, 20, 20, 0.0, 0.0, 0.0, 0.1, 0, 0, 0) self.s.end_atomic() self.s.begin_atomic()
def test_save_doc_in_frame(self): """Test saving many different crops of bigimage.ora""" cnt = 0 doc = document.Document() doc.load(join(paths.TESTS_DIR, 'bigimage.ora')) doc.set_frame_enabled(True) s = tiledsurface.Surface() t0 = time() positions = list(range(-1, +2)) positions.extend(range(-N - 1, -N + 2)) positions.extend(range(+N - 1, +N + 2)) def valid(c): x1, y1, x2, y2 = c return x1 < x2 and y1 < y2 def nth(g, n): _n = 0 for a in g: if _n == 0: yield a _n = (_n + 1) % n # Reduce number of frames when running on windows, to not run into # the "files opened" limit. The fact that we get this issue suggests # a problem with one of the save functions under Python 3, so it should # be investigated more thoroughly. n = 7 if "win" in sys.platform else 1 p = positions for x1, y1, x2, y2 in nth(filter(valid, product(p, repeat=4)), 7): cnt += 1 x, y, w, h = x1, y1, x2 - x1, y2 - y1 s.save_as_png('test_saveFrame_s.png', x, y, w, h) doc.update_frame(x=x, y=y, width=w, height=h) doc.save('test_saveFrame_doc.png') doc.save('test_saveFrame_doc.jpg') print( "saved %d frames in %0.2fs, " % (cnt, time() - t0), end="", file=sys.stderr, )
def reset(self): if self.surface: del self.surface self.time = 0 self.brush = self.get_brush(self.brush_path) self.surface = tiledsurface.Surface() # Draw box, add TILESIZE DISPLACEMENT self._border_paint(0, 0, 0) self._border_paint(0, 0, 1) self._border_paint(TILE_SIZE * (np.ceil(self.num_tiles) + 2), 0, 1) self._border_paint(TILE_SIZE * (np.ceil(self.num_tiles) + 2), TILE_SIZE * (np.ceil(self.num_tiles) + 2), 1) self._border_paint(0, TILE_SIZE * (np.ceil(self.num_tiles) + 2), 1) self._border_paint(0, 0, 1) self._border_paint(0, 0, 0) self.step = 0
def surface_alloc(): for i in iterations(): tiledsurface.Surface()
def __init__(self, logger=None, imsize=64, bg_color=None, max_episode_steps=10, pos_resolution=32, brush_info_file=None, start_x=0): super().__init__() # TODO: use EnvSpec? # length of a sequence self.tags = {'max_episode_steps': max_episode_steps} self.logger = logger or logging.getLogger(__name__) # starting positing of the pen for each episode self.tile_offset = mypaintlib.TILE_SIZE self.start_x = start_x self.start_color = (0, 0, 0) # initial color of the brush self.imsize = imsize self.pos_resolution = pos_resolution # action space self.action_space = spaces.Dict({ 'position': spaces.Discrete(self.pos_resolution**2), 'pressure': spaces.Box(low=0, high=1.0, shape=(), dtype=float), 'color': spaces.Box(low=0, high=1.0, shape=(3, ), dtype=float), 'prob': spaces.Discrete(2) }) # observation space self.observation_space = spaces.Dict({ 'image': spaces.Box(low=0, high=255, shape=(self.imsize, self.imsize, 3), dtype=np.uint8), 'position': spaces.Discrete(self.pos_resolution**2), 'pressure': spaces.Box(low=0, high=1.0, shape=(), dtype=float), 'color': spaces.Box(low=0, high=1.0, shape=(3, ), dtype=float), 'prob': spaces.Discrete(1) }) # color of the background if bg_color is None: self.bg_color = (1, 1, 1) else: self.bg_color = bg_color # open brush if brush_info_file is None: brush_info_file = os.getenv('BRUSHINFO') if brush_info_file is None: raise ValueError('You need to specify brush file') self.logger.debug('Open brush info from %s', brush_info_file) with open(brush_info_file, 'r') as f: self.brush_info = brush.BrushInfo(f.read()) self.brush = brush.Brush(self.brush_info) # initialize canvas (surface) self.surface = tiledsurface.Surface() # reset canvas and set current position of the pen self.reset()