summaryrefslogtreecommitdiff
path: root/lib/Pb/Generate/__init__.py
blob: 8e206dbb266307fc8b1e1b6bafd3c4db9ff06611 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python2.7
import sys
import os
from Config import *
from Pb import Pb
_default_tag = "im"

_gravity_params = ["NorthWest","North","NorthEast","West","Center","East","SouthWest","South","SouthEast"]
_gravity_default = "Center"
_compose_params = [ "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_params = ["None","Previous","Background"]
_dispose_default = "None"
class PbGenerate(Pb):
    example_params = {
#{{{ example params
     'nearest': 'true',
  #   'height': None,
     'compose': 'Soft_Light',
     'coalesce': 'true',
     'dispose': 'None',
     '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'
#}}}
    }
    def __init__(self, **kwargs):  
      super(PbGenerate,self).__init__(**kwargs);
      _definitions = { 
        #IMAGES
        "url": { 'type': "img_url" },
        "background": { 'type': "img_url" },

        #BOOLS
        "coalesce": { 'type': "bool" },
        "nearest": { 'type': "bool" },
        "merge_early": { 'type': "bool" },
        "flip": { 'type': "bool" },
        "flop": { 'type': "bool" },
        "tile": { 'type': "bool" },
        "transparent": { 'type': "bool" },

        #COLORS
        "black": { 'type': "color", 'default': 'black' },
        "white": { 'type': "color", 'default': 'white' },
        "subtract": { 'type': "color" },

        #INTS
        "fuzz": { 'type': "int" },
        "width": { 'type': "int" },
        "height": { 'type': "int" },
        "brightness": { 'type': "int" },
        "contrast": { 'type': "int" },
        "saturation": { 'type': "int" },
        "rotate": { 'type': "int" },
        "hue": { 'type': "int" },
        
        #ENUMS
        "compose": { 'type': "enum", 'enum_values': _compose_params, 'default': "Atop" },
        "gravity": { 'type': "enum", 'enum_values': _gravity_params, 'default': _gravity_default },
        "dispose": { 'type': "enum", 'enum_values': _dispose_params, 'default': "None" },
        "format": { 'type': "enum", 'enum_values': OUTPUT_IMAGE_TYPES, 'default': DEFAULT_FINALFORMAT },

        #STRINGS
        "username": { 'type': "string" },
        "callback": { 'type': "string" },
      }
      self.tag = _default_tag
      self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__);
      if self.params.background: self.tag = self.params.compose 
      if self.params.transparent: self.tag = self.params.transparent

      self.filename, self.filepath = self._filename_filepath_create(url=self.params.url['url'], extension=self.params.format)
      
      self._db_url_param = str(self.params.url['url'])

    def _composite (self):
      cmd = [
        BIN_CONVERT, self.params.background['path'], 
        "null:", self.filepath, "-matte",
        "-dispose", self.params.dispose,
        "-gravity", self.params.gravity,
        "-compose", self.params.compose, "-layers", "composite",
        self.filepath ]
      self._call_cmd(cmd);

    def _convert(self):
      cmd = [BIN_CONVERT, self.params.url['path'] ]
      if self.params.rotate: cmd += ["-rotate", self.params.rotate ]
      if self.params.flip: cmd += ["-flip"]    
      if self.params.flop: cmd += ["-flop"]    
      if self.params.transparent: 
        if self.params.fuzz: 
          cmd += ["-fuzz", "{}%".format(self.params.fuzz) ]
        cmd += [ "-transparent", self.params.subtract ] 
      if self.params.width or self.params.height:
        if self.params.nearest and self.params.format == "gif":
            cmd += [ "-coalesce","+map","-interpolate","Nearest","-interpolative-resize" ]
        else:
          cmd.append("-resize")
        cmd += [ "{}x{}".format(self.params.width or "", self.params.height or "") ]
      if self.params.black != "black" or self.params.white != 'white':
         cmd += [ "+level-colors" , "{},{}".format(self.params.black, self.params.white) ] 
      if self.params.contrast: cmd += [ '-contrast-stretch', self.params.contrast ] 
      if self.params.brightness or self.params.saturation or self.params.hue:
         cmd += [ 
           "-modulate", "{},{},{}".format( 
             (self.params.brightness or 100),
             (self.params.contrast or 100),
             (self.params.hue or 100)
           )] 
      cmd.append("-coalesce"); #why? #FIXME
      cmd += [ self.filepath ];
      self._call_cmd(cmd);
    
    def create(self):
      self._convert()
      if self.params.background:
        self._composite()
      self._cleanup();