Added metadata handling and a PNG metadata extraction tool

This commit is contained in:
jaseg 2013-04-06 16:07:29 +02:00
parent 76bc6482a6
commit e68a10fbae
2 changed files with 62 additions and 34 deletions

11
pngmeta.py Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env python
import os, sys, argparse
from PIL import Image, PngImagePlugin
parser = argparse.ArgumentParser(description='Print PNG metadata')
parser.add_argument('image', type=str)
args = parser.parse_args()
img = Image.open(args.image)
for k, v in img.info.items():
print('{:15}: {}'.format(k, v))

View file

@ -33,49 +33,66 @@ def parse_escape_sequence(seq):
def unpixelterm(text):
lines = text.split('\n')
metadata = {}
try:
first = lines.index('$$$')
second = lines[first+1:].index('$$$')
foo = [ line.split(': ') for line in lines[first+1:second] if line != '' ]
d = {}
for k,v in foo:
if k not in ['WIDTH', 'HEIGHT']:
d[k.lower()] = d.get(k.lower(), []) + [v]
metadata.update(d)
lines[first:] = lines[first+1+second+1:]
except:
pass
h = len(lines)*2
w = max([ len(re.sub(r'\x1b\[[0-9;]+m|\$.*\$', '', line)) for line in lines ])
img = Image.new('RGBA', (w, h))
fg, bg = (0,0,0,0), (0,0,0,0)
x, y = 0, 0
for escapeseq, specialstr, char, newline in re.findall(r'(\x1b\[[0-9;]+m)|(\$.*\$)|(.)|(\n)', text):
if escapeseq:
nfg, nbg = parse_escape_sequence(escapeseq)
fg, bg = nfg or fg, nbg or bg
elif specialstr:
if specialstr.startswith('$$$'): #metadata
pass
elif specialstr == '$\\$':
img.putpixel((x, y), (255, 0, 0, 127))
img.putpixel((x, y+1), (255, 0, 0, 127))
for line in lines:
for escapeseq, specialstr, char in re.findall(r'(\x1b\[[0-9;]+m)|(\$[^$]+\$)|(.)', line, re.DOTALL):
if escapeseq:
nfg, nbg = parse_escape_sequence(escapeseq)
fg, bg = nfg or fg, nbg or bg
elif specialstr:
if specialstr == '$\\$':
img.putpixel((x, y), (255, 0, 0, 127))
img.putpixel((x, y+1), (255, 0, 0, 127))
x += 1
elif specialstr == '$/$':
img.putpixel((x, y), (0, 0, 255, 127))
img.putpixel((x, y+1), (0, 0, 255, 127))
x += 1
else: #(should be a) balloon
bw = int(re.match(r'\$balloon([0-9]*)\$', specialstr).group(1))
for i in range(x, x+bw):
img.putpixel((i, y), (0, 255, 0, 127))
img.putpixel((i, y+1), (0, 255, 0, 127))
x += bw
elif char:
#Da magicks: ▀█▄
c = {' ': (bg, bg),
'': (fg, fg),
'': (fg, bg),
'': (bg, fg)}[char]
img.putpixel((x, y), c[0])
img.putpixel((x, y+1), c[1])
x += 1
elif specialstr == '$/$':
img.putpixel((x, y), (0, 0, 255, 127))
img.putpixel((x, y+1), (0, 0, 255, 127))
x += 1
else: #(should be a) balloon
bw = int(re.match(r'\$balloon([0-9]*)\$', specialstr).group(1))
for i in range(x, x+bw):
img.putpixel((i, y), (0, 255, 0, 127))
img.putpixel((i, y+1), (0, 255, 0, 127))
x += bw
elif char:
#Da magicks: ▀█▄
c = {' ': (bg, bg),
'': (fg, fg),
'': (fg, bg),
'': (bg, fg)}[char]
img.putpixel((x, y), c[0])
img.putpixel((x, y+1), c[1])
x += 1
else: #newline
x, y = 0, y+2
return img
x, y = 0, y+2
return img, metadata
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert images rendered by pixelterm-like utilities back to PNG')
parser.add_argument('input', type=argparse.FileType('r'))
parser.add_argument('output', type=str)
args = parser.parse_args()
img = unpixelterm(args.input.read())
img.save(args.output)
img, metadata = unpixelterm(args.input.read())
print('Metadata:')
pnginfo = PngImagePlugin.PngInfo()
for k, v in metadata.items():
print('{:15}: {}'.format(k, '/'.join(v)))
pnginfo.add_text(k, '/'.join(v))
img.save(args.output, 'PNG', pnginfo=pnginfo)