コード例 #1
0
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'))
コード例 #2
0
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'))
コード例 #3
0
ファイル: htmlcanvas.py プロジェクト: JasonGross/characters
def convert_strokes_to_images(stroke_list, dest_images, original_image_sizes=None, 
                              line_width=5, uncompressed_ext='.stroke', compressed_ext='.cstroke',
                              verbose=True, auto_resize=False, new_max_dimen=100, pad=2.5, scale_factor=1, **kargs):
    if original_image_sizes is None: original_image_sizes = [None for i in stroke_list]
    for stroke_name, original_image_size, new_image in zip(stroke_list, original_image_sizes, dest_images):
        if verbose: print("I'm saving %s..." % stroke_name)
        if stroke_name[-len(uncompressed_ext):].lower() == uncompressed_ext.lower():
            with open(stroke_name, 'r') as f:
                stroke = f.read().strip()
        elif stroke_name[-len(compressed_ext):].lower() == compressed_ext.lower():
            with open(stroke_name, 'rb') as f:
                cstroke = f.read()
            stroke = decompress_stroke(cstroke, to_string=False)
        else:
            print('Malformed file name: ' + stroke_name)
            continue
        size = None
        offset = None
        if auto_resize and original_image_size:
            original_width, original_height = original_image_size
            if original_width > original_height:
                scale_factor = new_max_dimen / float(original_width)
                width, height = new_max_dimen, original_height * scale_factor
            else:
                scale_factor = new_max_dimen / float(original_height)
                width, height = original_width * scale_factor, new_max_dimen
            if pad:
                width += math.ceil(2 * pad)
                height += math.ceil(2 * pad)
                offset = pad
            if width != height:
                offset = [offset, offset]
            if width > height:
                offset[1] += (width - height) / 2.0
                height = width
            elif height > width:
                offset[0] += (height - width) / 2.0
                width = height
            size = {'x':width, 'y':height}
        strokes_to_image(stroke, new_image, size=size, line_width=line_width, scale_factor=scale_factor, offset=offset, name=stroke_name)