django – 带有简单缩略图的衬垫
发布时间:2021-01-11 09:31:46 所属栏目:Python 来源:互联网
导读:我正在使用简易缩略图为我的网站制作缩略图.我想从1500x1023px的图像创建缩略图.所需缩略图的大小为100x100px.我想要的是缩略图显示整个徽标而不是裁剪或拉伸.我已经看到这被称为衬垫合身 – 与作物相反.例如,对于此图像,我们在顶部添加236px的空白,在底部添
|
我正在使用简易缩略图为我的网站制作缩略图.我想从1500x1023px的图像创建缩略图.所需缩略图的大小为100x100px.我想要的是缩略图显示整个徽标而不是裁剪或拉伸.我已经看到这被称为衬垫合身 – 与作物相反.例如,对于此图像,我们在顶部添加236px的空白,在底部添加237px的空白,然后调整大小.有没有办法用简单的缩略图来做到这一点?如果没有,有关如何处理此问题的任何建议?谢谢! 解决方法感谢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))
new_image.paste(image,(left,top))
return new_image (编辑:东莞站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python-2.7 – TypeError:zip参数#1必须支持迭代
- python – CherryPy日志记录:如何配置和使用全局和应用程序
- python – 在Pandas DataFrame中分割列表
- python – 如何动态调用类中的方法使用方法名分配给变量
- 如何在matplotlib中创建损坏的垂直条形图?
- python – 为什么pow(x,y)的时间复杂度为O(1),而x ** y为O(
- 使用带有嵌套查询的python MySQLDB SScursor
- python – Django不调用model clean方法
- 具有distinct()的Django order_by()过滤器
- python – 在Flask中重置cookie的到期时间
推荐文章
站长推荐
- python – 如何训练大型数据集进行分类
- python – hashlib.md5()TypeError:Unicode对象
- python-2.7 – TypeError:zip参数#1必须支持迭代
- 如何在Python中获取类属性的定义顺序?
- python-2.7 – 在Python 2.7中手动构建ConfigPar
- python – 如何访问`pip –user`安装的软件包?
- python exceptions.UnicodeDecodeError:’ascii
- python – 使用Numpy stride_tricks获取非重叠的
- Python“私有”名称修改和实例与类属性
- python – ElementTree find()/ findall()找不到
热点阅读
