Made a more sane png-metadata format
This commit is contained in:
parent
efbf9145c3
commit
97228be044
2 changed files with 17 additions and 21 deletions
13
pixelterm.py
13
pixelterm.py
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, argparse, os.path
|
||||
import os, sys, argparse, os.path, json
|
||||
#NOTE: This script uses pygments for RGB->X256 conversion since pygments is
|
||||
#readily available. If you do not like pygments (e.g. because it is large),
|
||||
#you could patch in something like https://github.com/magarcia/python-x256
|
||||
|
|
@ -87,12 +87,15 @@ if __name__ == '__main__':
|
|||
print(f)
|
||||
foo, _, _ = f.rpartition('.png')
|
||||
output = os.path.join(args.output_dir, os.path.basename(foo)+'.pony')
|
||||
metadata = '$$$\n' +\
|
||||
'\n'.join([ k.upper()+': '+img.info[k] for k in sorted(img.info.keys()) if k != 'comment' ]) +\
|
||||
'\n' + img.info.get('comment', '') +\
|
||||
metadata = json.loads(img.info.get('pixelterm-metadata'))
|
||||
comment = metadata['_comment']
|
||||
del metadata['_comment']
|
||||
metadataarea = '$$$\n' +\
|
||||
'\n'.join([ '\n'.join([ k.upper() + ': ' + v for v in metadata[k] ]) for k in sorted(metadata.keys()) ]) +\
|
||||
'\n' + comment +\
|
||||
'\n$$$\n'
|
||||
with open(output, 'w') as of:
|
||||
of.write(metadata)
|
||||
of.write(metadataarea)
|
||||
of.write(termify_pixels(img))
|
||||
else:
|
||||
print(termify_pixels(img))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, argparse, os.path
|
||||
import os, sys, argparse, os.path, json
|
||||
from collections import defaultdict
|
||||
#NOTE: This script uses pygments for X256->RGB conversion since pygments is
|
||||
#readily available. If you do not like pygments (e.g. because it is large),
|
||||
#you could patch in something like https://github.com/magarcia/python-x256
|
||||
|
|
@ -38,23 +39,20 @@ def parse_escape_sequence(seq):
|
|||
|
||||
def unpixelterm(text):
|
||||
lines = text.split('\n')
|
||||
metadata = {}
|
||||
metadata = defaultdict(list)
|
||||
try:
|
||||
first = lines.index('$$$')
|
||||
second = lines[first+1:].index('$$$')
|
||||
comment = []
|
||||
d = {}
|
||||
for l in lines[first+1:second+1]:
|
||||
metadataarea = lines[first+1:second+1]
|
||||
for i,l in enumerate(metadataarea):
|
||||
parts = l.split(': ')
|
||||
if len(parts) == 2:
|
||||
k,v = parts
|
||||
if k not in ['WIDTH', 'HEIGHT']:
|
||||
d[k.lower()] = d.get(k.lower(), []) + [v]
|
||||
metadata[k.lower()] += [v]
|
||||
else:
|
||||
comment.append(l)
|
||||
if comment:
|
||||
d['comment'] = d.get('comment', []) + ['\n'.join(comment)]
|
||||
metadata.update(d)
|
||||
metadata['_comment'] = '\n'.join(metadataarea[i:])
|
||||
break
|
||||
lines[first:] = lines[first+1+second+1:]
|
||||
except:
|
||||
pass
|
||||
|
|
@ -118,13 +116,8 @@ if __name__ == '__main__':
|
|||
if len(args.input) > 1:
|
||||
print(f.name)
|
||||
img, metadata = unpixelterm(f.read())
|
||||
if args.verbose:
|
||||
print('Metadata:')
|
||||
pnginfo = PngImagePlugin.PngInfo()
|
||||
for k, v in metadata.items():
|
||||
if args.verbose:
|
||||
print('{:15}: {}'.format(k, '/'.join(v)))
|
||||
pnginfo.add_text(k, '/'.join(v))
|
||||
pnginfo.add_text('pixelterm-metadata', json.dumps(metadata))
|
||||
foo, _, _ = f.name.rpartition('.pony')
|
||||
output = args.output or foo+'.png'
|
||||
if args.output_dir:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue