Don't misunderstand, I'm not admitting my mistake. I haven't yet given up on having 4K assets. But until I find a solution,..
This python script creates a clone of a given input folder at a given output location. In the cloned folder, all files are at a new resolution.
Executing this requires a certain level of comfort with command line to execute. If you want to use this script but don't know how, leave a reply here and I'll be happy to help.
Code: Select all
import os
from PIL import Image
from resizeimage import resizeimage
from resizeimage.imageexceptions import ImageSizeError
from shutil import copyfile
import sys, getopt
inputdir = ''
outputdir = ''
height = 0;
width = 0;
def printHelp(illegal):
if(illegal):
print 'Illegal argument detected. Allowed inputs are: '
print '-h: displays this'
print '-i: path to Ren\'Py project'
print '-o: path to clone to'
print '-h: heigth to resize to'
print '-w: width to resize to'
print 'NOTE: If a height and width is defined, only height will be used'
if (illegal):
sys.exit(2)
sys.exit(0)
def getArgs(argv):
global inputdir
global outputdir
global height
global width
try:
opts, args = getopt.getopt(argv,"i:o:h:w:",["idir=","odir=","height=","width=","help"])
except getopt.GetoptError:
printHelp(True)
for opt, arg in opts:
if opt == '--help':
print 'test.py -i <inputdir> -o <outputdir>'
printHelp(False)
elif opt in ("-i", "--ifile"):
inputdir = arg
elif opt in ("-o", "--ofile"):
outputdir = arg
elif opt in ("-h", "--height"):
height = int(arg)
elif opt in ("-w", "--width"):
width = int(arg)
if not inputdir or not outputdir or not (height or width):
print 'Please specify an input, an output and heigth or width. Call this file with --help for help.'
sys.exit(2)
return (inputdir, outputdir, height, width)
def ensuredir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def recursiveCrawl(path):
global inputdir, outputdir, height, width
projPath = inputdir + path
ensuredir(outputdir + path);
files = os.listdir(projPath)
print projPath
for file in files:
filePath = projPath + '/' + file;
if os.path.isdir(filePath):
recursiveCrawl(path + '/' + file)
elif file.lower().endswith(('.png', '.jpg', '.jpeg')):
resizeImage(path + '/', file)
else:
copyfile(filePath, outputdir + path + '/' + file)
def resizeImage(path, file):
global inputdir, outputdir, height, width
with open(inputdir + path + file, 'r+b') as f:
with Image.open(f) as image:
try:
if height:
newImage = resizeimage.resize_height(image, height)
newImage.save(outputdir + path + file, image.format)
elif width:
newImage = resizeimage.resize_width(image, height)
newImage.save(outputdir + path + file, image.format)
except ImageSizeError as err:
copyfile(inputdir + path + file, outputdir + path + file)
def main(argv):
args = getArgs(argv)
# Crawl through all folders in input folder
recursiveCrawl('')
if __name__ == "__main__":
main(sys.argv[1:])