Skip to content

Instantly share code, notes, and snippets.

@vampjaz
Last active June 2, 2018 21:43
Show Gist options
  • Save vampjaz/d0f977edb9540386eebf4ce2c4801535 to your computer and use it in GitHub Desktop.
Save vampjaz/d0f977edb9540386eebf4ce2c4801535 to your computer and use it in GitHub Desktop.
Reads a Minecraft texture file and its accompanying .mcmeta file and generates the specified animation
# depends on Pillow/PIL and ImageMagick's `convert' utility
from PIL import Image
import json
import sys
import os
import tempfile
import shutil
# specify the image file (not the mcmeta file) as the first argument
image_file = sys.argv[1]
mcmeta_file = image_file + '.mcmeta'
json_data = open(mcmeta_file,'r').read()
anim_data = json.loads(json_data).get('animation')
image = Image.open(image_file)
texture_size = image.width
def get_frame(framenum):
return image.crop((0,framenum*texture_size,texture_size,(framenum+1)*texture_size))
frames = []
for fnum in anim_data.get('frames',range(int(image.height / texture_size))):
frames.append(get_frame(fnum))
#frames[0].save(out_gif, save_all=True, append_images=frames[1:], duration=anim_data.get('frametime'))
# ^ does not work apparently, so we use IM
tempfolder = tempfile.mkdtemp()
out_gif = image_file + '.gif'
for index,frame in enumerate(frames):
frame.save("{}/{:03d}.png".format(tempfolder,index))
os.system('convert {}/*.png -set dispose previous -set delay {} -loop 0 {}'.format(tempfolder,anim_data.get('frametime',1)*10,out_gif))
shutil.rmtree(tempfolder)
@vampjaz
Copy link
Author

vampjaz commented Jun 2, 2018

it kinda works though i think the frame delay might be a bit off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment