From eb447c39c550322de11af033ddbca98cb9152f83 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 1 May 2014 20:58:48 +0200 Subject: [PATCH] [DEV] add resizer of the images --- lutinImage.py | 39 +++++++++++++++++++++++++++++++-------- lutinModule.py | 42 ++++++++++++++++++++++++++++++++++++------ lutinTarget.py | 25 +++++++++++++++++++------ lutinTargetAndroid.py | 2 +- 4 files changed, 87 insertions(+), 21 deletions(-) diff --git a/lutinImage.py b/lutinImage.py index f867dfd..ae1cfd1 100644 --- a/lutinImage.py +++ b/lutinImage.py @@ -1,13 +1,15 @@ #!/usr/bin/python import lutinDebug as debug +import lutinTools as tools import platform if platform.system() == "Darwin": import CoreGraphics else: - import image + from PIL import Image def resize(srcFile, destFile, x, y): + extension = destFile[destFile.rfind('.'):] if platform.system() == "Darwin": source_image = CoreGraphics.CGImageImport(CoreGraphics.CGDataProviderCreateWithFilename(srcFile)) source_width = source_image.getWidth() @@ -18,12 +20,33 @@ def resize(srcFile, destFile, x, y): colors = CoreGraphics.CGFloatArray(5) context = CoreGraphics.CGBitmapContextCreateWithColor(x, y, colors_space, colors) context.setInterpolationQuality(CoreGraphics.kCGInterpolationHigh) - new_image_rect = CoreGraphics.CGRectMake(0, 0, x, y) - context.drawImage(new_image_rect, new_image) - context.writeToFile(destFile, CoreGraphics.kCGImageFormatPNG) + if im1.size[0] <= x: + # for small image just copy: + tools.copy_file(srcFile, destFile) + else: + if y <= 0: + # keep ratio : + y = int(float(x) * float(source_height) / float(source_width)) + new_image_rect = CoreGraphics.CGRectMake(0, 0, x, y) + context.drawImage(new_image_rect, new_image) + tools.create_directory_of_file(destFile) + if extension == ".jpeg": + context.writeToFile(destFile, CoreGraphics.kCGImageFormatJPEG) + elif extension == ".png": + context.writeToFile(destFile, CoreGraphics.kCGImageFormatPNG) + else: + debug.error(" can not manage extention ... : " + destFile) else: # open an image file (.bmp,.jpg,.png,.gif) you have in the working folder - im1 = image.open(srcFile) - # use one of these filter options to resize the image - tmpImage = im1.resize((x, y), Image.ANTIALIAS) - tmpImage.save(destFile) + im1 = Image.open(srcFile) + if im1.size[0] <= x: + # for small image just copy: + tools.copy_file(srcFile, destFile) + else: + if y <= 0: + # keep ratio : + y = int(float(x) * float(im1.size[1]) / float(im1.size[0])) + # use one of these filter options to resize the image + tmpImage = im1.resize((x, y), Image.ANTIALIAS) + tools.create_directory_of_file(destFile) + tmpImage.save(destFile) diff --git a/lutinModule.py b/lutinModule.py index 245c406..62d566f 100644 --- a/lutinModule.py +++ b/lutinModule.py @@ -372,21 +372,50 @@ class Module: lutinMultiprocess.store_command(cmdLine, file_cmd) + ## + ## @brief Commands for copying files + ## + def image_to_staging(self, binaryName, target): + for source, destination, sizeX, sizeY in self.imageToCopy: + extension = source[source.rfind('.'):] + if extension != ".png" \ + and extension != ".jpg" \ + and sizeX > 0: + debug.error("Can not manage image other than .png and jpg to resize : " + source); + displaySource = source + source = self.originFolder + "/" + source + if destination == "": + destination = source[source.rfind('/')+1:] + debug.verbose("Regenerate Destination : '" + destination + "'") + if sizeX > 0: + debug.verbose("Image file : " + displaySource + " ==> " + destination + " resize=(" + str(sizeX) + "," + str(sizeY) + ")") + fileName, fileExtension = os.path.splitext(self.originFolder+"/" + source) + target.add_image_staging(source, destination, sizeX, sizeY) + else: + debug.verbose("Might copy file : " + displaySource + " ==> " + destination) + target.add_file_staging(source, destination) + ## ## @brief Commands for copying files ## def files_to_staging(self, binaryName, target): - for element in self.files: - debug.verbose("Might copy file : " + element[0] + " ==> " + element[1]) - target.add_file_staging(self.originFolder+"/"+element[0], element[1]) + for source, destination in self.files: + displaySource = source + source = self.originFolder + "/" + source + if destination == "": + destination = source[source.rfind('/')+1:] + debug.verbose("Regenerate Destination : '" + destination + "'") + # TODO : when destination is missing ... + debug.verbose("Might copy file : " + displaySource + " ==> " + destination) + target.add_file_staging(source, destination) ## ## @brief Commands for copying files ## def folders_to_staging(self, binaryName, target): - for element in self.folders: - debug.verbose("Might copy folder : " + element[0] + "==>" + element[1]) - lutinTools.copy_anything_target(target, self.originFolder+"/"+element[0],element[1]) + for source, destination in self.folders: + debug.verbose("Might copy folder : " + source + "==>" + destination) + lutinTools.copy_anything_target(target, self.originFolder + "/" + source, destination) # call here to build the module def build(self, target, packageName): @@ -480,6 +509,7 @@ class Module: return debug.verbose("build tree of " + self.name) # add all the elements (first added only one keep ==> permit to everload sublib element) + self.image_to_staging(packageName, target) self.files_to_staging(packageName, target) self.folders_to_staging(packageName, target) #build tree of all submodules diff --git a/lutinTarget.py b/lutinTarget.py index db81242..dc652c7 100644 --- a/lutinTarget.py +++ b/lutinTarget.py @@ -3,6 +3,7 @@ import lutinDebug as debug import datetime import lutinTools import lutinModule +import lutinImage class Target: def __init__(self, name, typeCompilator, debugMode, generatePackage, arch, cross, sumulator=False): @@ -97,19 +98,31 @@ class Target: def get_build_mode(self): return self.buildMode - def add_file_staging(self, inputFile, outputFile): - for source, dst in self.listFinalFile: + def add_image_staging(self, inputFile, outputFile, sizeX, sizeY): + for source, dst, x, y in self.listFinalFile: if dst == outputFile : debug.verbose("already added : " + outputFile) return debug.verbose("add file : '" + inputFile + "' ==> '" + outputFile + "'"); - self.listFinalFile.append([inputFile,outputFile]) + self.listFinalFile.append([inputFile,outputFile, sizeX, sizeY]) + + def add_file_staging(self, inputFile, outputFile): + for source, dst, x, y in self.listFinalFile: + if dst == outputFile : + debug.verbose("already added : " + outputFile) + return + debug.verbose("add file : '" + inputFile + "' ==> '" + outputFile + "'"); + self.listFinalFile.append([inputFile,outputFile, -1, -1]) def copy_to_staging(self, binaryName): baseFolder = self.get_staging_folder_data(binaryName) - for source, dst in self.listFinalFile: - debug.verbose("must copy file : '" + source + "' ==> '" + dst + "'"); - lutinTools.copy_file(source, baseFolder+"/"+dst) + for source, dst, x, y in self.listFinalFile: + if x == -1: + debug.verbose("must copy file : '" + source + "' ==> '" + dst + "'"); + lutinTools.copy_file(source, baseFolder+"/"+dst) + else: + debug.verbose("resize image : '" + source + "' ==> '" + dst + "' size=(" + str(x) + "," + str(y) + ")"); + lutinImage.resize(source, baseFolder+"/"+dst, x, y) def clean_module_tree(self): diff --git a/lutinTargetAndroid.py b/lutinTargetAndroid.py index 6756004..0aa3a28 100644 --- a/lutinTargetAndroid.py +++ b/lutinTargetAndroid.py @@ -295,7 +295,7 @@ class Target(lutinTarget.Target): lutinTools.create_directory_of_file(self.get_staging_folder(pkgName) + "/res/drawable/icon.png"); if "ICON" in pkgProperties.keys() \ and pkgProperties["ICON"] != "": - lutinTools.copy_file(pkgProperties["ICON"], self.get_staging_folder(pkgName) + "/res/drawable/icon.png", True) + lutinImage.resize(pkgProperties["ICON"], self.get_staging_folder(pkgName) + "/res/drawable/icon.png", 256, 256) else: # to be sure that we have all time a resource ... tmpFile = open(self.get_staging_folder(pkgName) + "/res/drawable/plop.txt", 'w')