Added metadata handling and a PNG metadata extraction tool
This commit is contained in:
parent
76bc6482a6
commit
e68a10fbae
2 changed files with 62 additions and 34 deletions
11
pngmeta.py
Executable file
11
pngmeta.py
Executable 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))
|
||||||
|
|
@ -33,49 +33,66 @@ def parse_escape_sequence(seq):
|
||||||
|
|
||||||
def unpixelterm(text):
|
def unpixelterm(text):
|
||||||
lines = text.split('\n')
|
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
|
h = len(lines)*2
|
||||||
w = max([ len(re.sub(r'\x1b\[[0-9;]+m|\$.*\$', '', line)) for line in lines ])
|
w = max([ len(re.sub(r'\x1b\[[0-9;]+m|\$.*\$', '', line)) for line in lines ])
|
||||||
img = Image.new('RGBA', (w, h))
|
img = Image.new('RGBA', (w, h))
|
||||||
fg, bg = (0,0,0,0), (0,0,0,0)
|
fg, bg = (0,0,0,0), (0,0,0,0)
|
||||||
x, y = 0, 0
|
x, y = 0, 0
|
||||||
for escapeseq, specialstr, char, newline in re.findall(r'(\x1b\[[0-9;]+m)|(\$.*\$)|(.)|(\n)', text):
|
for line in lines:
|
||||||
if escapeseq:
|
for escapeseq, specialstr, char in re.findall(r'(\x1b\[[0-9;]+m)|(\$[^$]+\$)|(.)', line, re.DOTALL):
|
||||||
nfg, nbg = parse_escape_sequence(escapeseq)
|
if escapeseq:
|
||||||
fg, bg = nfg or fg, nbg or bg
|
nfg, nbg = parse_escape_sequence(escapeseq)
|
||||||
elif specialstr:
|
fg, bg = nfg or fg, nbg or bg
|
||||||
if specialstr.startswith('$$$'): #metadata
|
elif specialstr:
|
||||||
pass
|
if specialstr == '$\\$':
|
||||||
elif specialstr == '$\\$':
|
img.putpixel((x, y), (255, 0, 0, 127))
|
||||||
img.putpixel((x, y), (255, 0, 0, 127))
|
img.putpixel((x, y+1), (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
|
x += 1
|
||||||
elif specialstr == '$/$':
|
x, y = 0, y+2
|
||||||
img.putpixel((x, y), (0, 0, 255, 127))
|
return img, metadata
|
||||||
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
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='Convert images rendered by pixelterm-like utilities back to PNG')
|
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('input', type=argparse.FileType('r'))
|
||||||
parser.add_argument('output', type=str)
|
parser.add_argument('output', type=str)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
img = unpixelterm(args.input.read())
|
img, metadata = unpixelterm(args.input.read())
|
||||||
img.save(args.output)
|
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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue