【问题标题】:Padded fit with easy thumbnails带有简单缩略图的衬垫合身
【发布时间】:2012-08-11 04:53:31
【问题描述】:

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

【问题讨论】:

标签: django thumbnails easy-thumbnails


【解决方案1】:

感谢 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

【讨论】:

    【解决方案2】:

    非常感谢乔希的回答!这正是我几周以来一直在寻找的东西。 但是,您的解决方案神秘地不适用于某些图像。

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

    【讨论】:

    • 很高兴它帮助了@Bertrand。除非您进行了重大更改,否则您可能会考虑简单地评论我的答案,而不是单独写一个答案。
    • 我知道,但我无法评论我以外的其他帖子。我想一个人需要更多的“声誉”来实现这一目标。
    猜你喜欢
    • 2016-06-20
    • 2020-12-03
    • 2011-10-04
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多