Created
November 28, 2017 00:05
-
-
Save SpaceManiac/50d783336422053b80fe142c5bb7eb39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys, shutil | |
from PIL import Image | |
from PIL.PngImagePlugin import PngInfo | |
def split(fname): | |
image = Image.open(f"{fname}.dmi") | |
if 'Description' not in image.info: | |
return | |
metadata = image.info['Description'] | |
line_iter = iter(metadata.splitlines()) | |
assert next(line_iter) == "# BEGIN DMI" | |
with open(f"{fname}.txt", "w") as f: | |
for each in line_iter: | |
if each.startswith("# END DMI"): | |
break | |
f.write(f"{each}\n") | |
shutil.copy(f"{fname}.dmi", f"{fname}.png") | |
print(f"Split {fname}.png and {fname}.txt") | |
def compose(fname): | |
with open(f"{fname}.txt") as f: | |
dmi = "# BEGIN DMI\n" + f.read().replace(" ", "\t") | |
pnginfo = PngInfo() | |
pnginfo.add_text('Description', dmi, zip=True) | |
Image.open(f"{fname}.png").save(f"{fname}.dmi", 'png', optimize=True, pnginfo=pnginfo) | |
print(f"Built {fname}.dmi") | |
for fname in sys.argv[1:]: | |
if fname.endswith('.dmi'): | |
split(fname[:-4]) | |
elif fname.endswith('.png') or fname.endswith('.txt'): | |
compose(fname[:-4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment