Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 软垫贴合,易于修剪指甲_Django_Thumbnails_Easy Thumbnails - Fatal编程技术网

Django 软垫贴合,易于修剪指甲

Django 软垫贴合,易于修剪指甲,django,thumbnails,easy-thumbnails,Django,Thumbnails,Easy Thumbnails,我正在使用简易缩略图为我的网站制作缩略图。我想从1500x1023px的图像创建一个缩略图。所需缩略图的大小为100x100px。我想要的是缩略图显示整个徽标,而不是裁剪或拉伸。我见过这种被称为“填充贴合”的东西,它是作物的反面。例如,对于这个图像,我们将在顶部添加236像素的空白,在底部添加237像素的空白,然后调整大小。有没有办法用简单的缩略图做到这一点?若否,有何建议?谢谢 感谢Timmy O'Mahony建议创建一个处理器来进行填充。这是你们中遇到类似问题的人的代码。要使其正常工作,您需

我正在使用简易缩略图为我的网站制作缩略图。我想从1500x1023px的图像创建一个缩略图。所需缩略图的大小为100x100px。我想要的是缩略图显示整个徽标,而不是裁剪或拉伸。我见过这种被称为“填充贴合”的东西,它是作物的反面。例如,对于这个图像,我们将在顶部添加236像素的空白,在底部添加237像素的空白,然后调整大小。有没有办法用简单的缩略图做到这一点?若否,有何建议?谢谢

感谢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(或任何位置)

导入图像
def pad_图像(图像,**kwargs):
“”“填充图像,使其与所需缩略图的纵横比相同。”。
"""
img_size=image.size
des_size=kwargs['size']
fit=[float(img_size[i])/des_size[i]用于范围(0,2)内的i]
如果拟合[0]>拟合[1]:
new_image=image.resize((image.size[0],
整数(圆形(尺寸[1]*fit[0]))
top=int((新的图像大小[1]-图像大小[1])/2)
左=0
elif拟合[0]<拟合[1]:
new_image=image.resize((int(圆形(des_size[0]*fit[1])),
图像大小[1]))
top=0
left=int((新的\u image.size[0]-image.size[0])/2)
其他:
返回图像
#为了透明
#掩码=图像.new('L',new_Image.size,color=0)
#新图像。putalpha(遮罩)
#白色的
新建_图像。粘贴((255、255、255、255))
新建图片。粘贴(图片,(左,上))
返回新的图像

非常感谢你的回答,乔希!这正是我几周来一直在寻找的。
但是,您的解决方案在某些图像上无法正常工作

以下是该缩略图处理器的(全功能)修订版:

如果不使用“裁剪”选项,会发生什么情况?(即创建缩略图所采取的步骤),因此您可以将其添加到中,这是一个很好的建议。谢谢我会用密码报告的。很高兴它对@Bertrand有帮助。除非你做出了重大改变,否则你可能会简单地评论我的答案而不是写一个单独的答案。我知道,但我无法评论其他帖子比我的。我想人们需要更多的“声誉”来实现这一点。
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