#!/usr/bin/python2.7 import sys import os from Config import * from Pb import Pb _gravity_params = ["NorthWest","North","NorthEast","West","Center","East","SouthWest","South","SouthEast"] _gravity_default = "Center" _compose_params = [ "Over", "ATop", "Dst_Over", "Dst_In", "Dst_Out", "Multiply", "Screen", "Divide", "Plus", "Difference", "Exclusion", "Lighten", "Darken", "Overlay", "Hard_Light", "Soft_Light", "Linear_Dodge", "Linear_Burn", "Color_Dodge", "Color_Burn" ] _dispose_params = ["None","Previous","Background"] _dispose_default = "None" class Im(Pb): example_params = { 'nearest': 'true', # 'height': None, 'compose': 'Soft_Light', 'coalesce': 'true', 'dispose': 'None', 'gravity': 'Center', 'width': '200', 'black': 'black', 'tile': 'true', 'white': 'white', 'contrast': '100', 'hue': '90', 'saturation': '100', 'merge_early': 'true', 'format': 'gif', 'background': 'http://i.asdf.us/im/bc/new_1430440747.gif', 'subtract': '#EE7AE9', 'transparent': 'true', # 'rotate': None, 'name': 'yo', # 'brightness': None, 'url': 'http://asdf.us/im/new.gif', 'flop': 'true', 'flip': 'false', 'callback': 'jsonp1430442384162', 'fuzz': '5' } def __init__(self, #URLS url=None, background=None, #BOOLS coalesce=None, dispose="None", nearest=None, merge_early=None, flip=None, flop=None, tile=None, transparent=None, #COLORS black=None, white=None, subtract=None, #INTS fuzz=None, width=None, height=None, brightness=None, contrast=None, saturation=None, rotate=None, hue=None, #ENUMS compose=None, gravity=_gravity_default, format=DEFAULT_FINALFORMAT, #STRINGS username=None, callback=None, ): #{{{ required_keys #IMAGES "url", "background", #BOOLS "coalesce", "dispose", "nearest", "merge_early", "flip", "flop", "tile", "transparent", #COLORS "black", "white", "subtract", #INTS "fuzz", "width", "height", "brightness", "contrast", "saturation", "rotate", "hue", #ENUMS "compose", "gravity", "format", #STRINGS "username", "callback", #}}} ] for k in self._required_keys: if k in kwargs: if k in [ 'url', 'background' ] and kwargs[k] != "" and kwargs[k] != None: self.params[k] = { 'url' : kwargs[k], 'filename' : self._make_tempname(k), 'path' : os.path.join(WORKING_DIR, self._make_tempname(k)) , } try: self.download(self.params[k]['url'], self.params[k]['path']) self.files_created.append(self.params[k]['path']) self.params[k]['mimetype'] = self.get_mimetype(self.params[k]['path']) except Exception as e: sys.stderr.write(str(e)) raise Exception ("BAD PARAMS"); elif k in [ 'black', 'white', 'subtract' ]: try: self.params[k] = self.is_color(kwargs[k]) except Exception: raise Exception("Unable to process color for:\n{}".format(k)) elif k in [ "coalesce", "dispose", "nearest", "merge_early", "flip", "flop", "tile", "transparent", ]: self.params[k] = self.bool_correct(self.sanitize(kwargs[k])) elif k == 'gravity' and self._test_enum(kwargs[k], GRAVITY_PARAMS): self.params[k] = kwargs[k] elif k == 'format' and self._test_enum(kwargs[k], FORMAT_PARAMS): self.params[k] = kwargs[k] elif k == 'compose' and self._test_enum(kwargs[k], COMPOSE_PARAMS): self.params[k] = kwargs[k] elif k == 'dispose' and self._test_enum(kwargs[k], DISPOSE_PARAMS): self.params[k] = kwargs[k] elif k in [ "fuzz", "width", "height", "brightness", "contrast", "saturation", "rotate", "hue" ]: if kwargs[k] == '': self.params[k] = None else: try: self.params[k] = str(int(kwargs[k])) except Exception as e: raise Exception("Problem with param {}:\n".format(k) + str(e)) else: self.params[k] = self.sanitize(kwargs[k]) if self.params.get('background'): self.tag = self.params.get('compose') else: self.tag = self.params.get('transparent', DEFAULT_TAG) self.basename = self._get_filename(); self.filename = "{}.{}".format(self.basename, self.params.get('format', DEFAULT_FINALFORMAT)) self.filepath = os.path.join(WORKING_DIR, self.filename) def _composite (self): cmd = [ BIN_CONVERT, self.params['background']['path'], "null:", self.filepath, "-matte", "-dispose", self.params.get('dispose', DISPOSE_DEFAULT), "-gravity", self.params.get("gravity",GRAVITY_DEFAULT), "-compose", self.params['compose'], "-layers", "composite", self.filepath ] self._call_cmd(cmd); def _convert(self): cmd = [BIN_CONVERT, self.params['url']['path'] ] if self.params.get('rotate'): cmd += ["-rotate", self.params['rotate'] ] if self.params.get('flip'): cmd += ["-flip"] if self.params.get('flop'): cmd += ["-flop"] if self.params.get('transparent'): if self.params.get('fuzz'): cmd += ["-fuzz", "{}%".format(self.params['fuzz']) ] cmd += [ "-transparent", self.params.get('subtract', "white") ] if self.params.get('width') or self.params.get('height'): if self.params.get('nearest') and self.params.get('format') == "gif": cmd += [ "-coalesce","+map","-interpolate","Nearest","-interpolative-resize" ] else: cmd.append("-resize") cmd += [ "{}x{}".format(self.params.get('width',"") or "", self.params.get('height',"") or "") ] if self.params.get('black') != "black" or self.params.get('white') != 'white': cmd += [ "+level-colors" , "{},{}".format(self.params.get('black','black'), self.params.get('white', 'white')) ] if self.params.get('contrast'): cmd += [ '-contrast-stretch', self.params['contrast'] ] if any( e in self.params.keys() for e in ['brightness', 'saturation', 'hue' ]): cmd += [ "-modulate", "{},{},{}".format( (self.params.get('brightness', 100) or 100), (self.params.get('contrast', 100) or 100), (self.params.get('hue', 100) or 100) )] cmd.append("-coalesce"); #why? #FIXME cmd += [ self.filepath ]; self._call_cmd(cmd); def create(self): self._convert() if self.params.get('background'): self._composite() self._cleanup();