diff options
Diffstat (limited to 'lib/pb/pattern.py')
| -rwxr-xr-x | lib/pb/pattern.py | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/lib/pb/pattern.py b/lib/pb/pattern.py index a9ab879..a17142c 100755 --- a/lib/pb/pattern.py +++ b/lib/pb/pattern.py @@ -1,41 +1,28 @@ -import os -import sys -import random -import re -import urllib -from config import * +from config import BIN_CONVERT, BIN_COMPOSITE from pb import Pb - -import simplejson as json from PIL import Image -import uuid -_fuse_mode="Pin_Light" +_FUSE_MODE="Pin_Light" class PbPattern(Pb): example_params = { - # "pattern_url" : "http://asdf.us/impattern/patterns/1.png", "pattern_data" : '{"matrix":[["0","0","0","0","0","1","0","0","0","0"],["0","0","0","0","1","1","1","0","0","0"],["0","0","1","1","1","0","1","0","0","0"],["0","1","1","0","0","0","0","0","0","0"],["0","1","0","0","1","0","0","0","0","0"],["0","1","0","0","1","0","0","0","1","0"],["0","1","0","0","1","1","0","0","1","0"],["0","1","0","0","0","1","1","1","1","0"],["0","1","1","1","1","0","0","0","0","0"],["0","0","0","0","1","0","0","0","0","0"]],"width":"10","height":"10"}', - # "username" : "garfield", "image_url" : "http://i.asdf.us/im/be/PinkHijab_1425078647_reye.gif", +# "username" : "garfield", +# "pattern_url" : "http://asdf.us/impattern/patterns/1.png", } - example_params = { - "username":"pepper", - "pattern_data":"", - "pattern_url":"http://asdf.us/impattern/patterns/63.png", - "image_url":"http://i.asdf.us/im/b4/PbGradientwhiteblack-PbGrid_1443161086_pepper.png", - - } def __init__(self, **kwargs): - super(PbPattern,self).__init__(**kwargs); + super(PbPattern, self).__init__(**kwargs) _definitions = { - 'image_url': { 'type':'img_url' }, - 'pattern_url': { 'type':'img_url' }, - 'pattern_data': { 'type':'json' }, - 'username': { 'type':'string' }, + 'image_url': {'type':'img_url'}, + 'pattern_url': {'type':'img_url'}, + 'pattern_data': {'type':'json'}, + 'username': {'type':'string'}, } - self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__); - self.filename, self.filepath = self._filename_filepath_create(url=self.params.image_url['url'], extension=self.params.image_url['mimetype']) + self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__) + self.filename, self.filepath = self._filename_filepath_create( + url=self.params.image_url['url'], extension=self.params.image_url['mimetype'] + ) if self.params.pattern_data: _pattern_filename, self._pattern_filepath = self._filename_filepath_create(namepart="pattern") self._from_pattern_data() @@ -50,18 +37,17 @@ class PbPattern(Pb): def _from_pattern_data(self): def boolToColor(boolean): if boolean: - return (0,0,0,255); + return (0, 0, 0, 255) else: - return (255,255,255,255) - specs = self.params.pattern_data.value; + return (255, 255, 255, 255) + specs = self.params.pattern_data.value if int(specs['width']) > 100 or int(specs['height']) > 100: - raise ValueError - sys.stderr.write("height and width need to be less than 100 px") - img = Image.new('RGBA', (int(specs['width']), int(specs['height']))); - pixels = img.load(); + self.err_warn("height and width need to be less than 100 px") + img = Image.new('RGBA', (int(specs['width']), int(specs['height']))) + pixels = img.load() for i in range(0, len(specs['matrix'])): for j in range(0, len(specs['matrix'][i])): - pixels[j,i] = boolToColor(int(specs['matrix'][i][j])); + pixels[j, i] = boolToColor(int(specs['matrix'][i][j])) img.save(self._pattern_filepath, "PNG") @@ -74,23 +60,27 @@ class PbPattern(Pb): #second step use the Canvas as a background def _make_mask(self): #tile the pattern pattern on the canvas - cmd = [BIN_COMPOSITE,"-tile", self._pattern_filepath, self.filepath, self.filepath]; + cmd = [BIN_COMPOSITE, "-tile", self._pattern_filepath, self.filepath, self.filepath] self._call_cmd(cmd) #fuse the tiled file to create a mask #convert thebg.gif -compose Dst_In null: thefile.gif -matte -layers composite new.gif - cmd = [BIN_CONVERT, self.filepath, "-compose", "Dst_In", "null:", - self.params.image_url['path'], "-matte", "-layers", "composite", self.filepath] + cmd = [ + BIN_CONVERT, self.filepath, "-compose", "Dst_In", "null:", + self.params.image_url['path'], "-matte", "-layers", "composite", self.filepath + ] self._call_cmd(cmd) #third step - def _fuse_mask(self, fuse_mode=_fuse_mode): - cmd = [BIN_CONVERT, "-dispose", "2", self.filepath, "null:", + def _fuse_mask(self, fuse_mode=_FUSE_MODE): + cmd = [ + BIN_CONVERT, "-dispose", "2", self.filepath, "null:", self.params.image_url['path'], "-matte", "-compose", fuse_mode, "-layers", "composite", - self.filepath] + self.filepath + ] self._call_cmd(cmd) def create(self): - self._make_canvas(); + self._make_canvas() self._make_mask() - self._fuse_mask(); + self._fuse_mask() super(PbPattern, self).create() |
