=== modified file 'phatch/actions/border.py' --- phatch/actions/border.py 2009-05-21 03:04:27 +0000 +++ phatch/actions/border.py 2009-06-13 09:08:49 +0000 @@ -25,20 +25,24 @@ def init(): global Image, ImageOps import Image, ImageOps - -def border(image, size=1, color=0): - """Adjust brightness from black to white - - size: -1(black) 0 (unchanged) 1(white) - - repeat: how many times it should be repeated""" - if size == 0: - return image - if size < 0: - #crop - image = ImageOps.crop(image,border=-size) + +def border(image, same_size_for_all=True, size=0, left_size=0, top_size=0, + bottom_size=0, right_size=0, color=0): + if same_size_for_all: + if size == 0: + return image + if size < 0: + return ImageOps.crop(image,border=-size) + else: + return ImageOps.expand(image,border=size,fill=color) else: - #expand - image = ImageOps.expand(image,border=size,fill=color) - return image + width, height = image.size + result_width = width + left_size + right_size + result_height = height + top_size + bottom_size + result = Image.new(image.mode, (result_width, result_height), color) + result.paste(image, (left_size, top_size)) + + return result #---Phatch class Action(models.Action): @@ -53,13 +57,44 @@ __doc__ = _t('Crop or add border to all sides') def interface(self,fields): + fields[_t('Same Size for All')] = self.BooleanField(True) fields[_t('Size')] = self.PixelField('1px',choices=['1','2','5']) + fields[_t('Left Size')] = self.PixelField('1px',choices=['1','2','5']) + fields[_t('Top Size')] = self.PixelField('1px',choices=['1','2','5']) + fields[_t('Bottom Size')] = self.PixelField('1px',choices=['1','2','5']) + fields[_t('Right Size')] = self.PixelField('1px',choices=['1','2','5']) fields[_t('Color')] = self.ColorField('#000000') - + + def get_relevant_field_labels(self): + """If this method is present, Phatch will only show relevant + fields. + + :returns: list of the field labels which are relevant + :rtype: list of strings + + .. note:: + + It is very important that the list of labels has EXACTLY + the same order as defined in the interface method. + """ + relevant = ['Same Size for All',] + if self.get_field_string('Same Size for All') in ('yes','true'): + relevant.append('Size') + else: + relevant.extend(['Left Size', 'Top Size', 'Bottom Size', 'Right Size']) + relevant.extend(['Color',]) + return relevant + def values(self,info): #pixel fields x0, y0 = info[new('Pil','Size')] - return super(Action,self).values(info,pixel_fields={'Size':(x0+y0)/2}) + avg = (x0+y0)/2 + return super(Action,self).values(info,pixel_fields={'Size':avg, + 'Left Size':avg, + 'Top Size':avg, + 'Bottom Size':avg, + 'Right Size':avg, + }) icon = \ 'x\xda\x01\xe5\x03\x1a\xfc\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\