diff options
author | jaseg <s@jaseg.de> | 2013-07-29 20:41:55 +0200 |
---|---|---|
committer | jaseg <s@jaseg.de> | 2013-07-29 20:43:45 +0200 |
commit | 76579920ef33af869ed9c643234fdb4a3dfbd7a1 (patch) | |
tree | e1e3e6e67e86380ab251198ed99458a83c1038a4 | |
parent | a1ce05f39b96f6992a8718ee4a192300f6db62dc (diff) | |
download | pixelterm-76579920ef33af869ed9c643234fdb4a3dfbd7a1.tar.gz pixelterm-76579920ef33af869ed9c643234fdb4a3dfbd7a1.tar.bz2 pixelterm-76579920ef33af869ed9c643234fdb4a3dfbd7a1.zip |
Added multiprocessing for bulk image conversion
-rwxr-xr-x | pixelterm | 49 |
1 files changed, 27 insertions, 22 deletions
@@ -1,31 +1,36 @@ #!/usr/bin/env python -import os, sys, argparse, os.path, json, pixelterm +import os, sys, argparse, os.path, json +import pixelterm +from multiprocessing import Pool from PIL import Image, PngImagePlugin +def convert(f): + img = Image.open(f).convert("RGBA") + if args.output_dir: + print(f) + foo, _, _ = f.rpartition('.png') + output = os.path.join(args.output_dir, os.path.basename(foo)+'.pony') + metadata = json.loads(img.info.get('pixelterm-metadata')) + comment = metadata.get('_comment') + if comment is not None: + del metadata['_comment'] + comment = '\n'+comment + else: + comment = '' + metadataarea = '$$$\n' +\ + '\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\ + comment + '\n$$$\n' + with open(output, 'w') as of: + of.write(metadataarea) + of.write(pixelterm.termify_pixels(img)) + else: + print(pixelterm.termify_pixels(img)) + if __name__ == '__main__': parser = argparse.ArgumentParser(description='Render pixel images on 256-color ANSI terminals') parser.add_argument('image', type=str, nargs='*') parser.add_argument('-d', '--output-dir', type=str, help='Output directory (if not given, output to stdout)') args = parser.parse_args() - for f in args.image: - img = Image.open(f).convert("RGBA") - if args.output_dir: - print(f) - foo, _, _ = f.rpartition('.png') - output = os.path.join(args.output_dir, os.path.basename(foo)+'.pony') - metadata = json.loads(img.info.get('pixelterm-metadata')) - comment = metadata.get('_comment') - if comment is not None: - del metadata['_comment'] - comment = '\n'+comment - else: - comment = '' - metadataarea = '$$$\n' +\ - '\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\ - comment + '\n$$$\n' - with open(output, 'w') as of: - of.write(metadataarea) - of.write(pixelterm.termify_pixels(img)) - else: - print(pixelterm.termify_pixels(img)) + p = Pool() + p.map(convert, args.image) |