Don't use Anchor internals; allow any anchor tuple

ResizeCanvas now uses the anchor behavior of the Crop processor
This commit is contained in:
Matthew Tretter 2012-02-17 23:04:45 -05:00
parent 568c3d29a1
commit a164427074
2 changed files with 21 additions and 4 deletions

View file

@ -291,4 +291,15 @@ class Anchor(object):
BOTTOM_LEFT: (0, 1),
BOTTOM: (0.5, 1),
BOTTOM_RIGHT: (1, 1),
}
}
@staticmethod
def get_tuple(anchor):
"""Normalizes anchor values (strings or tuples) to tuples.
"""
# If the user passed in one of the string values, convert it to a
# percentage tuple.
if anchor in Anchor._ANCHOR_PTS.keys():
anchor = Anchor._ANCHOR_PTS[anchor]
return anchor

View file

@ -126,12 +126,18 @@ class ResizeCanvas(object):
self.color = color or (255, 255, 255, 0)
def process(self, img):
new_img = Image.new('RGBA', (self.width, self.height), self.color)
original_width, original_height = img.size
if self.anchor:
x = int((self.width - img.size[0]) * Anchor._ANCHOR_PTS[self.anchor][0])
y = int((self.height - img.size[1]) * Anchor._ANCHOR_PTS[self.anchor][1])
anchor = Anchor.get_tuple(self.anchor)
trim_x, trim_y = self.width - original_width, \
self.height - original_height
x = int(float(trim_x) * float(anchor[0]))
y = int(float(trim_y) * float(anchor[1]))
else:
x, y = self.x, self.y
new_img = Image.new('RGBA', (self.width, self.height), self.color)
new_img.paste(img, (x, y))
return new_img