Added test to detect if items/blocks are animated and crops single frame from image if animated.

This commit is contained in:
ExpandingDev 2019-10-12 01:30:58 -04:00
parent e92140ce79
commit e831534bdb
1 changed files with 20 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import shutil, zipfile, sys, getopt, os, json, platform, csv, posixpath, subprocess import shutil, zipfile, sys, getopt, os, json, platform, csv, posixpath, subprocess
from PIL import Image
pack_format=1 pack_format=1
TEXTURE_SIZE = 64 TEXTURE_SIZE = 64
@ -58,6 +59,18 @@ def log(msg):
if debug: if debug:
logFile.write(msg + "\n") 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): 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 global magick_prefix, CONVERT, COMPOSITE, MOGRIFY, DISPLAY, ANIMATE, COMPARE, CONJURE, IDENTIFY, IMPORT, MONTAGE, STREAM
if magick_version == 7: if magick_version == 7:
@ -244,7 +257,7 @@ def clean_up():
#shutil.rmtree(input_directory) #shutil.rmtree(input_directory)
def crop_and_copy(): 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: with open(os.path.join("pack_formats", str(pack_format) + ".csv")) as formatCSV:
readCSV = csv.reader(formatCSV, delimiter=',') readCSV = csv.reader(formatCSV, delimiter=',')
for source_path, source_file, target_path, target_file, xs, ys, xl, yl, xt, yt, blacklisted in readCSV: 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 continue
if xs == "": if xs == "":
#Don't have to crop, just copy and rename #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: else:
#Use Image Magick to crop and then rename #Use Image Magick to crop and then rename
log("Cropping source file " + source_file) log("Cropping source file " + source_file)