diff options
| -rwxr-xr-x | generate.py | 268 | ||||
| -rw-r--r-- | move_to_server | 20 |
2 files changed, 138 insertions, 150 deletions
diff --git a/generate.py b/generate.py index 923b39a..fa0ea72 100755 --- a/generate.py +++ b/generate.py @@ -7,15 +7,19 @@ import sha from pb.config import * import pb.lib.utils as utils +#FIXME these guys can do stuff wider than 1000 LIKE_A_BOSS = "ryz pepper seamonkey JAMES".split(" ") DEFAULT_FINALFORMAT = "gif" DB_TAG = "pb"; GRAVITY = "NorthWest North NorthEast West Center East SouthWest South SouthEast".split(" ") +GRAVITY_DEFAULT = "center" FORMAT = "jpg gif png".split(" ") -COMPOSE = "Over ATop Dst_Over Dst_In Dst_Out Multiply Screen Divide Plus Difference Exclusion "+ - "Lighten Darken Overlay Hard_Light Soft_Light Pegtop_Light Linear_Light Vivid_Light Pin_Light "+ - "Linear_Dodge Linear_Burn Color_Dodge Color_Burn".split(" "); +COMPOSE = [ "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 = "None Previous Background".split(' '); class Generate(): def __init__(self, **kwargs): @@ -77,7 +81,7 @@ class Generate(): utils.download(self.params[k]['url'], self.params[k]['path']) self.files_created.append(self.params[k]['path']) self.params[k]['mimetype'] = utils.get_mimetype(self.params[k]['path']) - except Exception: + except Exception as e: sys.stderr.write(str(e)) raise Exception ("BAD PARAMS"); elif k in [ 'black', 'white', 'subtract' ]: @@ -89,15 +93,17 @@ class Generate(): "coalesce", "dispose", "nearest", "merge_early", "flip", "flop", "tile", "transparent", ]: - self.params[k] = utils.bootl_correct(utils.sanitize(kwargs[k])) - elif k == 'gravity' and self._test_enum(k, GRAVITY): + self.params[k] = utils.bool_correct(utils.sanitize(kwargs[k])) + elif k == 'gravity' and self._test_enum(kwargs[k], GRAVITY): + self.params[k] = kwargs[k] + elif k == 'format' and self._test_enum(kwargs[k], FORMAT): self.params[k] = kwargs[k] - elif k == 'format' and self._test_enum(k, FORMAT): + elif k == 'compose' and self._test_enum(kwargs[k], COMPOSE): self.params[k] = kwargs[k] - elif k == 'compose' and self._test_enum(k, COMPOSE): + elif k == 'dispose' and self._test_enum(kwargs[k], DISPOSE): self.params[k] = kwargs[k] elif k in [ "fuzz", "width", "height", "brightness", "contrast", "saturation", "rotate", "hue" ]: - self.params[k] = int(params[k]) + self.params[k] = int(kwargs[k]) else: self.params[k] = utils.sanitize(kwargs[k]) else: @@ -109,9 +115,13 @@ class Generate(): if not self.params.format: self.params.format = DEFAULT_FINALFORMAT + if not self.params.dispose: + self.params.dispose = 'None'; + self.filename = "{}.{}".format(self.basename, self.params.format) #final filepath is stored in self.filepath self.filepath = os.path.join(WORKING_DIR, self.filename) + print str(self.params); def _make_tempname(self, s): return "PBTMP{}{}".format(self.now, s); @@ -125,7 +135,7 @@ class Generate(): def _get_filename(self): return "{}_{}_{}".format( self.tag, - now(), + self.now, self.params.username or "" ); @@ -142,149 +152,107 @@ class Generate(): cmd = ["rm", "-f"] + self.files_created self._call_cmd(cmd) -def bin_composite (params, bottomdir, bottomfile, topdir, topfile, newfile): - cmd = [BIN_CONVERT] - cmd.append(os.path.join(bottomdir,bottomfile)) - cmd.append('null:') - cmd.append(os.path.join(topdir,topfile)) - cmd.append("-matte") - cmd.extend(params) - cmd.append("-layers composite") - cmd.append(os.path.join(topdir, newfile)) -# g.write("bin_composite command: %s" % (" ".join(cmd))); - os.system(" ".join(cmd)) - os.system("rm "+os.path.join(bottomdir,bottomfile)+" "+os.path.join(topdir,topfile)) - -def bin_convert (directory, params, oldfile, newfile): - cmd = [BIN_CONVERT, os.path.join(directory, oldfile)] - for x in params: - cmd.append(x) - cmd.append(os.path.join(directory,newfile)) - os.system(" ".join(cmd)) -# g.write("bin_convert command: %s" % (" ".join(cmd))); - os.system("rm "+os.path.join(directory,oldfile)) - - def _build_cmd(self): - cmd = [] - #FIXME test if number - if self.params.rotate: cmd += ["-rotate", self.params.rotate ] - if self.params.flip: cmd += ["-flip"] - if self.params.flop: cmd += ["-flop"] - -if param['transparent'] == "true": - tag = "transparent" - if is_number(param['fuzz']) and param['fuzz'] != 0: - cmd.append("-fuzz") - cmd.append(param['fuzz']+"%") - subtract_color = as_color(param['subtract'], "white") - cmd.append("-transparent") - cmd.append(subtract_color) - -VALID_DISPOSE_METHODS=["none","previous","background"] -dispose = "None" - -if param['width'] is not None and is_number(param['width']): - if int(param['width']) > 1000 and NOT_A_BOSS: - error ("width cannot be greater than 1000") - width = param['width'] -if param['height'] is not None and is_number(param['height']): - if int(param['height']) > 1000 and NOT_A_BOSS: - error ("height cannot be greater than 1000") - height = param['height'] - -if (width or height): - if param['nearest'] == "true": - if format == "gif": - cmd.append("-coalesce") - cmd.append("+map") - cmd.append("-interpolate") - cmd.append("Nearest") - cmd.append("-interpolative-resize") - else: - cmd.append("-resize") - -if width and height: - cmd.append(width + "x" + height) -elif width: - cmd.append(width) -elif height: - cmd.append("x" + height) +#FIXME need to be more specific two functions below + def bin_composite (self, params, bottomfile, topfile, newfile): + cmd = [BIN_CONVERT] + cmd += [ bottomfile ] + cmd += [ "null:" ] + cmd += [ topfile ] + cmd += [ "-matte" ] + cmd += params + cmd += [ "-layers", "composite" ] + cmd += [ newfile ] + self._call_cmd(cmd); -if param['black'] != "black" or param['white'] != 'white': - try: - black = as_color(param['black'], "black") - white = as_color(param['white'], "white") - cmd.append("+level-colors") - cmd.append(black+","+white) - except (): - pass + def bin_convert (self, params, oldfile, newfile): + cmd = [BIN_CONVERT, oldfile] + cmd.extend(params) + cmd.append(newfile) + self._call_cmd(cmd); -if param['contrast'] is not None and is_number(param['contrast']): - cmd.append("-contrast-stretch") - cmd.append(param['contrast']) - pass -if param['brightness'] is not None or param['saturation'] is not None or param['hue'] is not None: - bstring = '' - if is_number(param['brightness']): - bstring += param['brightness'] - else: - bstring += "100" - bstring += ',' - if is_number(param['contrast']): - bstring += param['contrast'] - else: - bstring += "100" - bstring += ',' - if is_number(param['hue']): - bstring += param['hue'] - if bstring != "100,100,": - cmd.append("-modulate") - cmd.append(bstring) - -if bgfile is not None: - tag = param['compose'] - gravity = param['gravity'] - - if gravity not in GRAVITY: - gravity = 'center' - - - compositefile = "composite_" + newfile - compositeparams = ["-dispose", "None", "-gravity", gravity] - compositeparams.extend([ "-compose", param['compose'] ]) - - cmd.append( "-coalesce") - bin_convert (WORKING_DIR, cmd, oldfile, compositefile) - bin_composite (compositeparams, WORKING_DIR, bgfile, WORKING_DIR, compositefile, newfile) - - insert_cmd(dir, oldfile, newfile, cmd, url, name, tag) - -else: - bin_convert(WORKING_DIR, cmd, oldfile, newfile) - insert_cmd(dir, oldfile, newfile, cmd, url, name, tag) - -# jsonp callback -if param['callback'] is not None: - url = (BASE_URL+dir+"/"+newfile).replace("'", "\\'") - width, height = bin_identify (WORKING_DIR, newfile) - print param['callback'] + "({" - print "'url':'" + url + "'," - print "'size':" + str(file_size (WORKING_DIR, newfile)) + "," - print "'width':" + width + "," - print "'height':" + height - print "});" - moveToS3(os.path.join(WORKING_DIR, newfile), "im/"+dir+"/"+newfile) -else: - print "#@im" - print ", ".join([k+"="+str(v) for k,v in param.iteritems()]) - print " ".join(cmd) - print BASE_URL+dir+"/"+newfile - print file_size (WORKING_DIR, newfile) - print bin_identify (WORKING_DIR, newfile) - moveToS3(os.path.join(WORKING_DIR, newfile), "im/"+dir+"/"+newfile) + def _build_cmd(self): + cmd = [] + if self.params.rotate: cmd += ["-rotate", self.params.rotate ] + if self.params.flip: cmd += ["-flip"] + if self.params.flop: cmd += ["-flop"] + #FIXME add like a boss and make width == height when there's only one + #same thing in the other scripts + #also dotdict is bad, it's like javascript + if self.params['transparent']: + self.tag = "transparent" + if self.params.fuzz: + cmd += ["-fuzz", "{}%".format(self.params.fuzz) ] + cmd += [ "-transparent", self.params.get('subtract', "white") ] + if self.params.width or self.params.height: + if self.params['nearest']: + if self.params.format == "gif": + cmd += [ "-coalesce","+map","-interpolate","Nearest","-interpolative-resize" ] + else: + cmd.append("-resize") + cmd.append("{}x{}".format(self.params.get('width',''), self.params.get('height',''))) + if self.params['black'] != "black" or self.params['white'] != 'white': + cmd += [ "+level-colors" , "{},{}".format(self.params.black, self.params.black) ] + if self.params.contrast: + cmd += [ '-contrast-stretch', self.params.contrast ] + if any( e in self.params.keys for e in ['brightness', 'saturation', 'hue' ]): + cmd += [ + "{},{},{}".format( + self.params.get('brightness', 100), + self.params.get('contrast', 100), + self.params.get('hue', 100) + ), + "-modulate" ] + if self.params.bgfile: + self.tag = self.params['compose'] -#remove(newfile) -g.close() + cmd.append("-coalesce"); + compositeparams = ["-dispose", "None", "-gravity", self.params.get("gravity",GRAVITY_DEFAULT) ] + compositeparams.extend([ "-compose", self.params['compose'] ]) +# +# bin_convert (WORKING_DIR, cmd, oldfile, compositefile) +#then... +# bin_composite (compositeparams, bgfile, compositefile, newfile) +# +# insert_cmd(dir, oldfile, newfile, cmd, url, name, tag) +# +#else: +# bin_convert(WORKING_DIR, cmd, oldfile, newfile) +# insert_cmd(dir, oldfile, newfile, cmd, url, name, tag) +# +##remove(newfile) +# +#g.close() +if __name__ == "__main__": + TEST_PARAMS = { + 'nearest': 'true', +# 'height': None, + 'compose': 'Soft_Light', + 'coalesce': 'true', + 'dispose': 'true', + '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' + } + g = Generate(**TEST_PARAMS); + diff --git a/move_to_server b/move_to_server new file mode 100644 index 0000000..0cc890c --- /dev/null +++ b/move_to_server @@ -0,0 +1,20 @@ +## jsonp callback +#if param['callback'] is not None: +# url = (BASE_URL+dir+"/"+newfile).replace("'", "\\'") +# width, height = bin_identify (WORKING_DIR, newfile) +# print param['callback'] + "({" +# print "'url':'" + url + "'," +# print "'size':" + str(file_size (WORKING_DIR, newfile)) + "," +# print "'width':" + width + "," +# print "'height':" + height +# print "});" +# moveToS3(os.path.join(WORKING_DIR, newfile), "im/"+dir+"/"+newfile) +#else: +# print "#@im" +# print ", ".join([k+"="+str(v) for k,v in param.iteritems()]) +# print " ".join(cmd) +# print BASE_URL+dir+"/"+newfile +# print file_size (WORKING_DIR, newfile) +# print bin_identify (WORKING_DIR, newfile) +# moveToS3(os.path.join(WORKING_DIR, newfile), "im/"+dir+"/"+newfile) +# |
