def compress_all_strokes_in_current_directory(uncompressed_ext='.stroke', compressed_ext='.cstroke', remove_old=True, verbose=True, show_compression=True): for base, dirs, files in os.walk(os.getcwd()): if verbose: print("I'm in " + base) for file_name in files: if file_name[-len(uncompressed_ext):] == uncompressed_ext: if not show_compression: print('Compressing %s...' % file_name) with open(os.path.join(base, file_name), 'rb') as f: stroke = f.read() new_stroke = compress_stroke(stroke) with open(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext)), 'wb') as f: f.write(new_stroke) if show_compression: print('Compressed %s at\t\t%.2f%%' % (file_name, 100.0 * len(new_stroke) / len(stroke))) if remove_old: os.rename(os.path.join(base, file_name), os.path.join(base, file_name + '.bak')) # Testing to make sure that we can actually decompress the new file correctly before deleting the old file with open(os.path.join(base, file_name + '.bak'), 'rb') as f: old_stroke = f.read() with open(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext)), 'rb') as f: new_stroke = f.read() new_old_stroke = decompress_stroke(new_stroke) if new_old_stroke.strip() != old_stroke.strip() and stroke_to_list(new_old_stroke) != stroke_to_list(old_stroke): os.rename(os.path.join(base, file_name + '.bak'), os.path.join(base, file_name)) os.remove(os.path.join(base, file_name.replace(uncompressed_ext, compressed_ext))) print('Error on stroke %s in %s' % (file_name, base)) print('I wanted:') print(stroke_to_list(old_stroke)) print('I got:') print(stroke_to_list(new_old_stroke)) print('The compressed version is:') print(repr(new_stroke)) raw_input('Press enter or ^C') else: os.remove(os.path.join(base, file_name + '.bak'))
def decompress_all_strokes_in_current_directory(uncompressed_ext='.stroke', compressed_ext='.cstroke', remove_old=True, verbose=True, show_compression=True): for base, dirs, files in os.walk(os.getcwd()): if verbose: print("I'm in " + base) for file_name in files: if file_name[-len(compressed_ext):] == compressed_ext: if not show_compression: print('Decompressing %s...' % file_name) with open(os.path.join(base, file_name), 'rb') as f: stroke = f.read() new_stroke = decompress_stroke(stroke) with open( os.path.join( base, file_name.replace(compressed_ext, uncompressed_ext)), 'wb') as f: f.write(new_stroke) if show_compression: print('Decompressed %s at\t\t%.2f%%' % (file_name, 100.0 * len(stroke) / len(new_stroke))) if remove_old: os.rename(os.path.join(base, file_name), os.path.join(base, file_name + '.bak')) # Testing to make sure that we can actually decompress the new file correctly before deleting the old file with open(os.path.join(base, file_name + '.bak'), 'rb') as f: old_stroke = f.read() with open( os.path.join( base, file_name.replace(compressed_ext, uncompressed_ext)), 'rb') as f: new_stroke = f.read() new_old_stroke = compress_stroke(new_stroke) if new_old_stroke != old_stroke and stroke_to_list( new_old_stroke) != stroke_to_list(old_stroke): os.rename(os.path.join(base, file_name + '.bak'), os.path.join(base, file_name)) os.remove( os.path.join( base, file_name.replace(uncompressed_ext, compressed_ext))) print('Error on stroke %s in %s' % (file_name, base)) print('I wanted:') print(repr(old_stroke)) print('I got:') print(repr(new_old_stroke)) print('The uncompressed version is:') print(new_stroke) raw_input('Press enter or ^C') else: os.remove(os.path.join(base, file_name + '.bak'))
def _fix_strokes(stroke_string): rtn = stroke_to_list(stroke_string) min_t = int(rtn[0][0]['t']) for stroke in rtn: for point in stroke: point['t'] = int(point['t']) - min_t return '[' + ','.join('[' + ','.join("{'x':%(x)s,'y':%(y)s,'t':%(t)d}" % point for point in stroke) + ']' for stroke in rtn) + ']'
def _fix_strokes(stroke_string): rtn = stroke_to_list(stroke_string) min_t = int(rtn[0][0]['t']) for stroke in rtn: for point in stroke: point['t'] = int(point['t']) - min_t return '[' + ','.join( '[' + ','.join("{'x':%(x)s,'y':%(y)s,'t':%(t)d}" % point for point in stroke) + ']' for stroke in rtn) + ']'
def paint_image_with_strokes_cairo(strokes, line_width=5, line_cap='round', image=None, size=None, line_join='miter', scale_factor=1, offset=None, tuple_to_dict=(lambda t: {'x':t[0], 'y':t[1]}), smooth=2, name=None, ctx=None, clear=True): ## import cPickle ## print(list(map(repr, [strokes, line_width, line_cap, image, size, line_join, scale_factor, offset]))) ## cPickle.dump('paint_image_with_strokes_cairo' + repr((strokes, line_width, line_cap, image, size, line_join, scale_factor, offset)), ## open(os.path.expanduser('~/Desktop/temp.args'), 'wb')) if isinstance(strokes, str): strokes = stroke_to_list(strokes, parsed_to_int=True) if isinstance(scale_factor, (int, float)): scale_factor = (scale_factor, scale_factor) if isinstance(offset, (int, float)): offset = (offset, offset) if isinstance(size, (int, float)): size = (size, size) if image and not size: size = (image.get_height(), image.get_width()) if not isinstance(scale_factor, dict): scale_factor = tuple_to_dict(tuple(scale_factor)) if offset is not None and not isinstance(offset, dict): offset = tuple_to_dict(tuple(offset)) if size is not None and not isinstance(size, dict): size = tuple_to_dict(tuple(size)) if offset is None: offset = {'x':math.ceil(line_width / 2.0), 'y':math.ceil(line_width / 2.0)} strokes = [[{'x':point['x']*scale_factor['x'] + offset['x'], 'y':point['y']*scale_factor['y'] + offset['y']} \ for point in stroke] for stroke in strokes] x_max = max(max(point['x'] for point in stroke) for stroke in strokes) y_max = max(max(point['y'] for point in stroke) for stroke in strokes) if size is None: size = {'x':x_max + 1 + math.ceil(line_width / 2.0), 'y':y_max + 1 + math.ceil(line_width / 2.0)} elif size['x'] < x_max or size['y'] < y_max: os.system('echo "%s: Image size %s too small for stroke size %s. Scale factor is %s." >> %s' % \ (name, size, (x_max, y_max), scale_factor, os.path.join(RESULTS_PATH, 'image_warnings.log'))) warn('Image size %s too small for stroke size %s. Scale factor is %s.' % (size, (x_max, y_max), scale_factor)) ## raw_input('Press enter to adjust the scale factor...') scale_factor = max((size['x'], size['y'])) / float(max((x_max, y_max))) strokes = [[{'x':(point['x'] - offset['x']) * scale_factor + offset['x'], 'y':(point['y'] - offset['y']) * scale_factor + offset['y']} \ for point in stroke] for stroke in strokes] if smooth: strokes = [[stroke[0]] + \ [{'x':average(pt['x'] for pt in stroke[i:i+smooth]), 'y':average(pt['y'] for pt in stroke[i:i+smooth])} for i in range(len(stroke) - smooth)] + \ [stroke[-1]] for stroke in strokes] ## import cPickle ## cPickle.dump(strokes, open(os.path.expanduser('~/Desktop/temp.strokes'), 'wb')) width, height = int(math.ceil(size['x'])), int(math.ceil(size['y'])) if not image: image = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) if not ctx: ctx = cairo.Context(image) ctx.set_antialias(cairo.ANTIALIAS_NONE) if clear: # Make the background white ctx.set_source_rgb(1.0, 1.0, 1.0) ctx.rectangle(0, 0, width, height) ctx.fill() ctx.set_line_cap(_LINE_CAPS[line_cap]) ctx.set_line_join(_LINE_JOINS[line_join]) ctx.set_line_width(line_width) ctx.set_source_rgb(0, 0, 0) # make the stroke color black for stroke in strokes: x, y = stroke[0]['x'], stroke[0]['y'] ctx.rectangle(x - line_width / 2.0, y - line_width / 2.0, line_width, line_width) ctx.fill() ## image.write_to_png(os.path.expanduser('~/Desktop/temp.png')); raw_input() ctx.move_to(x, y) for point in stroke: x, y = point['x'], point['y'] ctx.line_to(x, y) ctx.stroke() ## image.write_to_png(os.path.expanduser('~/Desktop/temp.png')); raw_input() return image, ctx