def whitespace(image, size, whitespace=False, whitespace_color=None, **kwargs):
if not whitespace:
return image
if whitespace_color is None:
whitespace_color = FILER_WHITESPACE_COLOR
if whitespace_color is None:
whitespace_color = '#fff'
old_image = image
source_x, source_y = image.size
target_x, target_y = size
image = Image.new('RGBA', (target_x, target_y), whitespace_color)
if source_x < target_x and source_y < target_y: # whitespace all around
image.paste(old_image, (
(target_x - source_x) / 2, (target_y - source_y) / 2))
elif source_x < target_x: # whitespace on top and bottom only
image.paste(old_image, ((target_x - source_x) / 2, 0))
elif source_y < target_y: # whitespace on sides only
image.paste(old_image, (0, (target_y - source_y) / 2))
else: # no whitespace needed
image = old_image
return image
评论列表
文章目录