Python生成具有透明性和固定宽度的缩略图

2024-03-29 08:58:39 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用Python/Djangosorl-thumbnail库生成缩略图,我被要求生成一个150x150的图像,类似于这样(如果您看不到顶部和底部有一些透明的边距:

Image to generate

我试着做:

get_thumbnail(
            original_image, '150x150', quality=99, format='PNG'
        )

我可以用sorl-thumbnail来做吗??我的意思是在顶部和底部添加透明胶片,并将整个图像大小保持在150x150?如果不是,我如何用另一个python包实现这一点?你知道吗

谢谢


Tags: 图像imageformatgetpngsorlthumbnailquality
1条回答
网友
1楼 · 发布于 2024-03-29 08:58:39

我不知道关于一些缩略图,但你可以做它的形象。你基本上需要创建一个透明的150x150图像,并把你的缩略图放在上面。你知道吗

#!/usr/bin/python

from PIL import Image

margin=20
X=150
Y=150
in_path="flower.jpg"
out_path="thumbnail.png"

#creates a transparent background, RGBA mode, and size 150 by 150.
background = Image.new('RGBA', (X,Y))


# opening an image and converting to RGBA:
img = Image.open(in_path).convert('RGBA')

# Resizing the image

img = img.resize((X, Y-margin*2), Image.ANTIALIAS)

# Putting thumbnail on background

background.paste(img, (0, margin), img)
background.save(out_path)

顶部和底部带有透明条纹的输出:

enter image description here

相关问题 更多 >