summaryrefslogtreecommitdiff
path: root/Pb/Gradient/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Pb/Gradient/__init__.py')
-rwxr-xr-xPb/Gradient/__init__.py217
1 files changed, 217 insertions, 0 deletions
diff --git a/Pb/Gradient/__init__.py b/Pb/Gradient/__init__.py
new file mode 100755
index 0000000..5120c7d
--- /dev/null
+++ b/Pb/Gradient/__init__.py
@@ -0,0 +1,217 @@
+#!/usr/bin/python2.7
+import re
+import time
+from subprocess import call
+import simplejson as json
+import sys
+import os
+import sha
+from Config import *
+from Pb import Pb
+
+PARAM_LIST = [
+ "width", "height",
+ "color1", "color2",
+ "stripes",
+ "stripenumber", "stripeintensity",
+ "blurriness",
+ "contrast",
+ "brightness", "saturation", "hue",
+ "halftone",
+ "bevel", "percentbeveled",
+ "rotate", "flip", "flop", "tilt",
+ "filetype",
+ "gradienttype",
+ "username",
+]
+DEFAULT_FORMAT = "png"
+DEFAULT_COLORS = {
+ "color1" : "white",
+ "color2" : "black",
+};
+
+DEFAULT_WIDTH = "200"
+DEFAULT_HEIGHT = "200"
+DEFAULT_BEVEL_PERCENT = "12";
+
+HALFTONEVALUES = {
+ "checkeredfade": "h6x6a",
+ "etchedtransition": "o8x8",
+ "bendaydots": "h16x16o",
+ "smallerdots1": "h8x8o",
+ "smallerdots2": "c7x7w",
+ "flatstripes": "o2x2",
+ }
+
+
+class Gradient(Pb):
+ def __init__(self, **kwargs):
+ self.tag = "imGradient"
+ self.directory = WORKING_DIR
+ self.commands = []
+ self.filename = ""
+ self.filepath = ""
+ self._now = self.now()
+
+ params = {}
+ for key in PARAM_LIST:
+ if key in kwargs:
+ if key in ['color1', 'color2']:
+ params[key] = self.is_color(kwargs[key])
+ else:
+ params[key] = self.sanitize(kwargs[key])
+
+ if key in ['rotate','tilt','blurriness','stripenumber','stripeintensity']:
+ params[key] = params[key] if self.is_number(params[key]) else ""
+ elif key in ['brightness', 'contrast', 'hue']:
+ if not self.is_number(params[key]) or params[key] == "100": params[key] = ""
+ else:
+ params[key] = ""
+ params['width'] = params['width'] if self.is_number(params['width']) else DEFAULT_WIDTH
+ params['height'] = params['height'] if self.is_number(params['height']) else DEFAULT_HEIGHT
+ params["color1"] = params["color1"] or DEFAULT_COLORS["color1"];
+ params["color2"] = params["color2"] or DEFAULT_COLORS["color2"];
+ self.params = params
+ if not self.params['percentbeveled']: self.params['percentbeveled'] = DEFAULT_BEVEL_PERCENT
+ self._bevelvalues = [
+ "flatout", "flatinner", "evenlyframed", "biginner",
+ "bigouter", "dramaticflatout", "dramaticflatinner",
+ ]
+
+ def newfilename(self):
+ return "{}{}-{}_{}_{}.{}".format(
+ self.tag,
+ self.params['color1'].replace('#','').replace('(','-').replace(')','-'),
+ self.params['color2'].replace('#','').replace('(','-').replace(')','-'),
+ self._now,
+ self.params['username'],
+ self.params['filetype'] or DEFAULT_FORMAT,
+ )
+
+ def _call_cmd(self, cmd):
+ try:
+ self.call_cmd(cmd)
+ self.commands.append(" ".join(cmd));
+ except Exception:
+ raise Exception("Unable to call cmd {}".format(str(cmd)))
+
+
+ def _build_cmd(self):
+ self.cmd = [BIN_CONVERT]
+ self.cmd.extend([
+ '-size',
+ "{}x{}".format(self.params["width"],self.params["height"])
+ ])
+
+ if self.params['rotate']: self.cmd.extend(["-rotate", self.params["rotate"]])
+ if self.params['tilt']: self.cmd.extend(["-distort","SRT",self.params['tilt']])
+ if self.params['flip'] == "true": self.cmd.append("-flip")
+ if self.params['flop'] == "true": self.cmd.append("-flop")
+ if self.params['contrast']: self.cmd.extend(["-contrast-stretch", self.params['contrast']])
+ gradients = {
+ "canvas" : ["canvas:{}".format(self.params['color1'])],
+ "radial" : [
+ "radial-gradient:{}-{}".format( self.params['color1'], self.params['color2'])
+ ],
+ "colorspace" : [
+ "-colorspace",
+ "Gray",
+ "plasma:{}-{}".format(self.params['color1'], self.params['color2'])
+ ],
+ "mirrored" : [
+ "plasma:{}-{}".format(self.params['color1'], self.params['color2']),
+ "\(","+clone","-flop","\)",
+ "append"
+ ],
+ "plasmawash" : [
+ "plasma:{}-{}".format(self.params['color1'], self.params['color2']),
+ "-set","colorspace","HSB"
+ ],
+ "gradientwash" : [
+ "gradient:{}-{}".format(self.params['color1'], self.params['color2']),
+ "-set","colorspace","HSB"
+ ],
+ "noise" : ["xc:","+noise","Random","-virtual-pixel","tile"]
+ }
+ if self.params["gradienttype"] in gradients:
+ self.cmd.extend(gradients[self.params['gradienttype']])
+ else:
+ self.cmd.append("gradient:{}-{}".format(self.params['color1'], self.params['color2']))
+
+ if self.params['blurriness']:
+ self.cmd.extend(["-blur","0x{}".format(self.params["blurriness"]),"-auto-level"])
+
+ if self.params['stripes'] == "true" and len(self.params['stripenumber']):
+ self.cmd.extend(["-function","Sinusoid"])
+ if self.params['stripeintensity']:
+ self.cmd.append("{},{}".format(self.params['stripenumber'],self.params["stripeintensity"]))
+ else:
+ self.cmd.append(self.params['stripenumber'])
+ if self.params["halftone"] in HALFTONEVALUES:
+ self.cmd.extend([
+ "-ordered-dither",
+ HALFTONEVALUES[self.params["halftone"]]
+ ])
+ self.cmd += [
+ '-modulate',
+ "{},{},{}".format(
+ self.params['brightness'] or "100",
+ self.params['saturation'] or "100",
+ self.params['hue'] or "100")
+ ]
+ self.cmd.append(os.path.join(self.directory,self.filename));
+ self._call_cmd(self.cmd)
+
+ def _get_bevelvalue(self):
+ w, h = map(int, (self.params['width'], self.params['height']))
+ if h >= w:
+ bevpercentval = str(int(self.params['percentbeveled'])*0.005*int(h))
+ else:
+ bevpercentval = str(int(self.params['percentbeveled'])*0.005*int(w))
+ return {
+ "flatout": ["-s",bevpercentval,"-m","outer"],
+ "flatinner": ["-s",bevpercentval,"-m","inner"],
+ "evenlyframed": ["-s ",bevpercentval,"-m", "split"],
+ "biginner": ["-s",bevpercentval,"-m","outer","-c","50","-b","red","-a","25"],
+ "bigouter": ["-s",bevpercentval,"-m","split","-c","50","-b","red","-a","25"],
+ "dramaticflatout": ["-s",bevpercentval,"-m","outer","-a","25","-b","blue"],
+ "dramaticflatinner": ["-s",bevpercentval,"-m","outer","-a","25","-b","blue"],
+ }[self.params['bevel']]
+
+ def _make_bevel(self):
+ cmd = [BEVELBORDER]
+ cmd += self._get_bevelvalue()
+ cmd += [ os.path.join(self.directory,self.filename), os.path.join(self.directory, self.filename) ]
+ self._call_cmd(cmd)
+
+ def create(self):
+ self.filename = self.newfilename()
+ self.filepath = os.path.join(self.directory, self.filename)
+ self._build_cmd()
+# sys.stderr.write(str(self.cmd))
+
+ if self.params['bevel'] in self._bevelvalues:
+ self._make_bevel()
+
+if __name__ == "__main__":
+ TEST_FORM = {
+ "width" : "200",
+ "color1" : "#ffdead",
+ "color2" : "blue",
+ "stripes" : "true",
+ "stripenumber" : "20",
+ "gradienttype" : "radial",
+ "stripeintensity" : "20",
+ "halftone" : "checkeredfade",
+ "percentbeveled" : "30",
+ "flip" : "true",
+ "bevel" : "flatinner",
+ "rotate" : "20",
+ "height" : "200",
+ "filetype" : "jpg",
+ "username" : "whatever"
+ }
+ g = Gradient(**TEST_FORM);
+ g.create();
+ print " ".join(g.commands)
+ print g.filename