From e831534bdb03cb3e825caa72f5a702efda094798 Mon Sep 17 00:00:00 2001 From: ExpandingDev Date: Sat, 12 Oct 2019 01:30:58 -0400 Subject: [PATCH] Added test to detect if items/blocks are animated and crops single frame from image if animated. --- convert-mc-resource-pack.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/convert-mc-resource-pack.py b/convert-mc-resource-pack.py index 839c8d5..0e47a77 100755 --- a/convert-mc-resource-pack.py +++ b/convert-mc-resource-pack.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import shutil, zipfile, sys, getopt, os, json, platform, csv, posixpath, subprocess +from PIL import Image pack_format=1 TEXTURE_SIZE = 64 @@ -58,6 +59,18 @@ def log(msg): if debug: logFile.write(msg + "\n") +# This function is called to check if an image is animated. Returns True if it is animated, false if it is a static image +def is_animated(imagePath): + global TEXTURE_SIZE + im = Image.open(imagePath) + width, height = im.size + if width == height: + return False + elif height % width == 0: # Height is an even factor of width, so it's probably animated frames of an item/block, so make sure we crop it. + return True + else: # Height is not an even factor of the width, so it probably isn't animated and is a mob texture + return False + def crop_image(xs,ys,xl,yl,xt,yt,src,dst): global magick_prefix, CONVERT, COMPOSITE, MOGRIFY, DISPLAY, ANIMATE, COMPARE, CONJURE, IDENTIFY, IMPORT, MONTAGE, STREAM if magick_version == 7: @@ -244,7 +257,7 @@ def clean_up(): #shutil.rmtree(input_directory) def crop_and_copy(): - global input_directory, pack_format, output_directory, generating_for_game + global input_directory, pack_format, output_directory, generating_for_game, TEXTURE_SIZE with open(os.path.join("pack_formats", str(pack_format) + ".csv")) as formatCSV: readCSV = csv.reader(formatCSV, delimiter=',') for source_path, source_file, target_path, target_file, xs, ys, xl, yl, xt, yt, blacklisted in readCSV: @@ -264,7 +277,12 @@ def crop_and_copy(): continue if xs == "": #Don't have to crop, just copy and rename - shutil.copyfile(source, destination) + #Ensure that item/block is not animated, if it is animated, crop it. + if is_animated(source): + warn("Source file " + source_file + " seems to be animated, cropping a single tile...") + crop_image(0,0,TEXTURE_SIZE,TEXTURE_SIZE,0,0,source,destination) + else: + shutil.copyfile(source, destination) else: #Use Image Magick to crop and then rename log("Cropping source file " + source_file)