diff options
Diffstat (limited to 'lib/pb/grid.py')
| -rwxr-xr-x | lib/pb/grid.py | 127 |
1 files changed, 75 insertions, 52 deletions
diff --git a/lib/pb/grid.py b/lib/pb/grid.py index 53d31df..0fafd1c 100755 --- a/lib/pb/grid.py +++ b/lib/pb/grid.py @@ -3,7 +3,8 @@ import re import os import simplejson as json import random -from config import DEFAULT_FINALFORMAT, DEFAULT_HEIGHT, DEFAULT_WIDTH, WORKING_DIR, OUTPUT_IMAGE_TYPES +from config import DEFAULT_FINALFORMAT, DEFAULT_HEIGHT,\ + DEFAULT_WIDTH, WORKING_DIR, OUTPUT_IMAGE_TYPES from config import THREEDROTATE, GRID, BIN_CONVERT, BIN_COMPOSITE import tempfile from pb import Pb @@ -22,53 +23,75 @@ class PbGrid(Pb): 'trim' : 'true' } def __init__(self, **kwargs ): - super(PbGrid,self).__init__(**kwargs); - _definitions = { - 'width': { 'type':'int' }, - 'height': { 'type':'int' }, - 'linethickness': { 'type':'int', 'default': 1 }, - 'opacity': { 'type':'float', "default": 1.0 }, - 'linecolor': { 'type':'color', 'default': 'whitesmoke' }, - 'linethickness': { 'type':'int' }, - 'spacing': { 'type':'int', 'default': 10 }, - 'vlines': { 'type':'bool' }, - 'hlines': { 'type':'bool' }, - 'shadow': { 'type':'bool' }, - 'bgimage': { 'type':'img_url' }, - 'bgcolor': { 'type':'color', 'default': 'transparent' }, - 'imageinstead': { 'type':'img_url' }, - 'planebgcolor': { 'type':'color', 'default': 'transparent' }, - 'planebgimage': { 'type':'img_url' }, - 'swing': { 'type':'int' }, - 'tilt': { 'type':'int' }, - 'roll': { 'type':'int' }, - 'zoom': { 'type':'int' }, - 'skycolor': { 'type':'color', 'default': 'transparent' }, - 'transition': { 'type':'enum', 'enum_values' : ['background', 'dither', 'edge', 'mirror', 'random', 'tile'], 'default': 'background' }, - 'trim': { 'type':'bool' }, - 'finalformat': { 'type':'enum', 'enum_values' : OUTPUT_IMAGE_TYPES, 'default': DEFAULT_FINALFORMAT }, - 'username': { 'type':'string' }, + super(PbGrid,self).__init__(**kwargs) + _definitions = { + 'width': {'type':'int'}, + 'height': {'type':'int'}, + 'linethickness': {'type':'int', 'default': 1}, + 'opacity': {'type':'float', "default": 1.0}, + 'linecolor': {'type':'color', 'default': 'whitesmoke'}, + 'linethickness': {'type':'int'}, + 'spacing': {'type':'int', 'default': 10}, + 'vlines': {'type':'bool'}, + 'hlines': {'type':'bool'}, + 'shadow': {'type':'bool'}, + 'bgimage': {'type':'img_url'}, + 'bgcolor': {'type':'color', 'default': 'transparent'}, + 'imageinstead': {'type':'img_url'}, + 'planebgcolor': {'type':'color', 'default': 'transparent'}, + 'planebgimage': {'type':'img_url'}, + 'swing': {'type':'int'}, + 'tilt': {'type':'int'}, + 'roll': {'type':'int'}, + 'zoom': {'type':'int'}, + 'skycolor': {'type':'color', 'default': 'transparent'}, + 'transition': { + 'type':'enum', + 'enum_values' :[ + 'background', 'dither', 'edge', 'mirror', 'random', 'tile' + ], + 'default': 'background' + }, + 'trim': {'type':'bool'}, + 'finalformat': { + 'type':'enum', + 'enum_values' :OUTPUT_IMAGE_TYPES, + 'default': DEFAULT_FINALFORMAT + }, + 'username': {'type':'string'}, } - self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__); + self.params.definitions_import( + _definitions, kwargs, classname=self.__class__.__name__ + ) if self.params.imageinstead: - self.filename, self.filepath = self._filename_filepath_create(url=self.params.imageinstead['url'], extension=self.params.finalformat) + self.filename, self.filepath = self._filename_filepath_create( + url=self.params.imageinstead['url'], extension=self.params.finalformat + ) elif self.params.planebgimage: - self.filename, self.filepath = self._filename_filepath_create(url=self.params.planebgimage['url'], extension=self.params.finalformat) + self.filename, self.filepath = self._filename_filepath_create( + url=self.params.planebgimage['url'], extension=self.params.finalformat + ) else: - self.filename, self.filepath = self._filename_filepath_create(extension=self.params.finalformat) + self.filename, self.filepath = self._filename_filepath_create( + extension=self.params.finalformat + ) - self._db_url_param = str(filter(lambda n: n, [ self.params.imageinstead, self.params.planebgimage, self.params.bgimage, "NULL"])[0]) + self._db_url_param = str( + filter( + lambda n: n, [self.params.imageinstead, self.params.planebgimage, self.params.bgimage, "NULL"] + )[0] + ) #makes a canvas file...step 1 (if not bgimage) def _make_canvas(self): dimensions = "{}x{}".format( - self.params.width or DEFAULT_WIDTH, + self.params.width or DEFAULT_WIDTH, self.params.height or DEFAULT_HEIGHT ) if self.params.bgimage: - return + return bgcolor = "xc:{}".format(self.params.bgcolor or 'transparent') - cmd = [ BIN_CONVERT, "-size", dimensions, bgcolor, self.filepath ] + cmd = [BIN_CONVERT, "-size", dimensions, bgcolor, self.filepath] self._call_cmd(cmd) #2nd step-- run grid @@ -77,18 +100,18 @@ class PbGrid(Pb): if self.params.spacing: if self.params.vlines: width = 2 * int(self.params.width or DEFAULT_WIDTH) - cmd += ["-s","{},{}".format(self.params.spacing,width)] + cmd += ["-s", "{},{}".format(self.params.spacing, width)] elif self.params.hlines: height = 2 * int(self.params.height or DEFAULT_HEIGHT) - cmd += ["-s", "{},{}".format(height,self.params.spacing)] + cmd += ["-s", "{},{}".format(height, self.params.spacing)] else: - cmd += ["-s",self.params.spacing] + cmd += ["-s", self.params.spacing] cmd += [ "-c", self.params.linecolor or _default_line_color] - if self.params.linethickness: cmd += ['-t',self.params.linethickness] - if self.params.opacity: cmd += ['-o',self.params.opacity] + if self.params.linethickness: cmd += ['-t', self.params.linethickness] + if self.params.opacity: cmd += ['-o', self.params.opacity] cmd += [ self.filepath, self.filepath ] self._call_cmd(cmd) - + def _shadow_cmd(self): #convert 1.png \( +clone -background black -shadow 110x1+9+9 \) +swap -background none -layers merge +repage 2.png cmd = [ @@ -99,15 +122,15 @@ class PbGrid(Pb): self.filepath ] self._call_cmd(cmd) - - + + def _threed_rotate_cmd (self): #3rd step--run 3Drotate cmd = [THREEDROTATE] if self.params.swing: cmd += ["pan={}".format(self.params.swing)] if self.params.tilt: cmd += ["tilt={}".format(self.params.tilt)] if self.params.roll: cmd += ["roll={}".format(self.params.roll)] - if self.params.zoom: + if self.params.zoom: cmd += ["zoom={}".format(self.params.zoom)] if cmd == [THREEDROTATE]: #if nothing has been added return @@ -124,27 +147,27 @@ class PbGrid(Pb): def _trim_cmd (self): cmd = [BIN_CONVERT, self.filepath, "-trim", "+repage", self.filepath] self._call_cmd(cmd) - + def _prepare_gridimage(self, image): if image['mimetype'] == 'gif': - _frame = self._choose_gif_frame(image['path']) + _frame = self._choose_gif_frame(image['path']) if image['mimetype'] != 'png': cmd = [BIN_CONVERT, image['path'], self.filepath] else: cmd = ['cp', image['path'], self.filepath] self._call_cmd(cmd) - + def _overlay_planebgimage(self): - cmd = [ + cmd = [ BIN_COMPOSITE, - "-compose", "Dst_Over", "-gravity", "center", - self.params.planebgimage["path"], + "-compose", "Dst_Over", "-gravity", "center", + self.params.planebgimage["path"], self.filepath, self.filepath ] self._call_cmd(cmd) - + def create(self): if self.params.bgimage: self._prepare_gridimage(self.params.bgimage) @@ -157,5 +180,5 @@ class PbGrid(Pb): if self.params.shadow: self._shadow_cmd() self._threed_rotate_cmd() if self.params.planebgimage: self._overlay_planebgimage() - if self.params.trim: self._trim_cmd() + if self.params.trim: self._trim_cmd() super(PbGrid, self).create() |
