Created
January 21, 2018 21:09
-
-
Save buganini/5d5cd6fdefb479f64128ec379088bfb9 to your computer and use it in GitHub Desktop.
Script for converting gif to pngs with android animation xml
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 | |
from subprocess import check_output as exe | |
import sys | |
import os | |
import math | |
gif = sys.argv[1] | |
out = sys.argv[2] | |
if not os.path.exists(out): | |
os.makedirs(out) | |
l = exe(["identify", "-format", "%s:%T\n", gif]).decode("utf-8").strip() | |
frames = [] | |
for f in l.split("\n"): | |
i, cs = f.split(":") | |
frames.append(int(cs)*10) | |
dn = math.ceil(math.log(len(frames), 10)) | |
exe(["convert", gif, "-coalesce", "{}/{}_%0{}d.png".format(out, out, dn)]) | |
with open("{}/{}.xml".format(out, out), "w") as f: | |
f.write("""<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">\n""") | |
for i, ms in enumerate(frames): | |
sn = "{{:0{}d}}".format(dn).format(i) | |
f.write(""" <item android:drawable="@drawable/{}_{}" android:duration="{}" />\n""".format(out, sn, ms)) | |
f.write("""</animation-list>""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment