带有简单缩略图的衬垫配合

时间:2012-08-14 20:17:32

标签: django thumbnails easy-thumbnails

我正在使用简易缩略图为我的网站制作缩略图。我想从1500x1023px的图像创建缩略图。所需缩略图的大小为100x100px。我想要的是缩略图显示整个徽标而不是裁剪或拉伸。我已经看到这被称为衬垫合身 - 与作物相反。例如,对于此图像,我们在顶部添加236px的空白,在底部添加237px的空白,然后调整大小。有没有办法用简单的缩略图来做到这一点?如果没有,有关如何处理此问题的任何建议?谢谢!

2 个答案:

答案 0 :(得分:9)

感谢Timmy O'Mahony建议创建一个处理器来进行填充。这是针对类似问题的那些代码。为了实现这一点,您需要在设置中使用以下内容:

THUMBNAIL_PROCESSORS = (
    'easy_thumbnails.processors.colorspace',
    'common.thumbnail_processors.pad_image',
    'easy_thumbnails.processors.autocrop',
    'easy_thumbnails.processors.scale_and_crop',
    'easy_thumbnails.processors.filters')

然后你可以将它添加到common / thumbnail_processors.py(或任何地方)

import Image

def pad_image(image, **kwargs):
    """ Pad an image to make it the same aspect ratio of the desired thumbnail.
    """

    img_size = image.size
    des_size = kwargs['size']
    fit = [float(img_size[i])/des_size[i] for i in range(0,2)]

    if fit[0] > fit[1]:
        new_image = image.resize((image.size[0],
            int(round(des_size[1]*fit[0]))))
        top = int((new_image.size[1] - image.size[1])/2)
        left = 0
    elif fit[0] < fit[1]:
        new_image = image.resize((int(round(des_size[0]*fit[1])), 
            image.size[1]))
        top = 0
        left = int((new_image.size[0] - image.size[0])/2)
    else:
        return image

    # For transparent
    #mask=Image.new('L', new_image.size, color=0)
    #new_image.putalpha(mask)

    # For white
    new_image.paste((255, 255, 255, 255))

    new_image.paste(image, (left, top))
    return new_image

答案 1 :(得分:3)

非常感谢你的回答,Josh!这正是我几个星期以来所寻找的。 但是,您的解决方案神秘地不适用于某些图像。

这是缩略图处理器的(完全功能)修订版: https://bitbucket.org/bercab/cmsplugin-nivoslider/src/b07db53c1ce4/cmsplugin_nivoslider/thumbnail_processors.py

相关问题