1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
import os
import sys
import random
import re
import urllib
from Config import *
from Pb import Pb
import simplejson as json
from PIL import Image
import uuid
_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",
}
def __init__(self,
pattern_url=None,
pattern_data=None,
username=None,
image_url=None,
):
super(PbPattern,self).__init__();
self.params.set_val("image_url", url, value_type="img_url");
self.params.set_val("pattern_url", url, value_type="img_url");
self.params.set_val("pattern_data", url, value_type="json");
self.params.set_val("username", username, value_type="string");
self.basename, self._format = self._get_filename();
#FIXME omit file extension for downloaded files
#lets go back to this in a second
self._pattern_file = os.path.join(WORKING_DIR, "IMPATTERNTMP_PTN{}_{}.{}".format(self.basename, self._pid, self._format)) # this
self._download(self.params.image_url, self._downloaded_file)
self.filename = "{}.{}".format(self.basename, self._format)
self.filepath = os.path.join(WORKING_DIR, self.filename)
if self.params['pattern_url']:
self._download(self.params['pattern_url'], self._pattern_file)
elif self.params['pattern_data']:
self._from_pattern_data()
else:
sys.stderr.write("pattern must be supplied as json array or as a png url")
raise ValueError;
def _from_pattern_data(self):
def boolToColor(boolean):
if boolean:
return (0,0,0,255);
else:
return (255,255,255,255)
specs = json.loads(self.params.pattern_data);
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();
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]));
img.save(self._pattern_file, "PNG")
#first step
def _make_canvas(self):
_width, _height = self.dimensions(self._downloaded_file) # same here
cmd = [BIN_CONVERT, "-size", _width + "x" + _height, "canvas:transparent", self.filepath]
self._call_cmd(cmd)
#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_file, 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._downloaded_file, "-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:",
self._downloaded_file, "-matte", "-compose", fuse_mode, "-layers", "composite",
self.filepath]
self._call_cmd(cmd)
def create(self):
self._make_canvas();
self._make_mask()
self._fuse_mask();
|