PIL:缩略图并最终得到方形图像

发布于 2024-08-04 10:33:27 字数 255 浏览 5 评论 0原文

调用

image = Image.open(data)
image.thumbnail((36,36), Image.NEAREST)

将保持宽高比。但我最终需要像这样显示图像:

<img src="/media/image.png" style="height:36px; width:36px" />

我可以使用图像周围透明或白色的信箱样式吗?

Calling

image = Image.open(data)
image.thumbnail((36,36), Image.NEAREST)

will maintain the aspect ratio. But I need to end up displaying the image like this:

<img src="/media/image.png" style="height:36px; width:36px" />

Can I have a letterbox style with either transparent or white around the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

放低过去 2024-08-11 10:33:27

PIL 已经有一个函数可以做到这一点:

from PIL import Image, ImageOps
thumb = ImageOps.fit(image, size, Image.LANCZOS)

PIL already has a function to do exactly that:

from PIL import Image, ImageOps
thumb = ImageOps.fit(image, size, Image.LANCZOS)
世态炎凉 2024-08-11 10:33:27

将图像粘贴到具有正确大小的透明图像作为背景

from PIL import Image
size = (36, 36)
image = Image.open(data)
image.thumbnail(size, Image.LANCZOS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
    image, (int((size[0] - image.size[0]) / 2), int((size[1] - image.size[1]) / 2))
)
background.save("output.png")

编辑:修复语法错误

Paste the image into a transparent image with the right size as a background

from PIL import Image
size = (36, 36)
image = Image.open(data)
image.thumbnail(size, Image.LANCZOS)
background = Image.new('RGBA', size, (255, 255, 255, 0))
background.paste(
    image, (int((size[0] - image.size[0]) / 2), int((size[1] - image.size[1]) / 2))
)
background.save("output.png")

EDIT: fixed syntax error

擦肩而过的背影 2024-08-11 10:33:27
from PIL import Image

import StringIO

def thumbnail_image():
    image = Image.open("image.png")
    image.thumbnail((300, 200))
    thumb_buffer = StringIO.StringIO()
    image.save(thumb_buffer, format=image.format)
    fp = open("thumbnail.png", "w")
    fp.write(thumb_buffer.getvalue())
    fp.close()
from PIL import Image

import StringIO

def thumbnail_image():
    image = Image.open("image.png")
    image.thumbnail((300, 200))
    thumb_buffer = StringIO.StringIO()
    image.save(thumb_buffer, format=image.format)
    fp = open("thumbnail.png", "w")
    fp.write(thumb_buffer.getvalue())
    fp.close()
茶花眉 2024-08-11 10:33:27

更新Cesar Canassa 的答案

这将创建 image.jpg 的缩略图为 image_thumb.jpg

from PIL import Image, ImageOps
fname = 'image.jpg'
size = (48,48)
thumb = ImageOps.fit(Image.open(fname), size, Image.LANCZOS)
thumb.save('{}_thumb.jpg'.format(fname[:fname.rfind('.')]), "JPEG")

Update of Cesar Canassa's answer.

This will create a thumbnail of image.jpg as image_thumb.jpg:

from PIL import Image, ImageOps
fname = 'image.jpg'
size = (48,48)
thumb = ImageOps.fit(Image.open(fname), size, Image.LANCZOS)
thumb.save('{}_thumb.jpg'.format(fname[:fname.rfind('.')]), "JPEG")
迷途知返 2024-08-11 10:33:27

或者这个,也许......(原谅意大利面条)

from PIL import Image

def process_image(image, size):
    if image.size[0] > size[0] or image.size[1] > size[1]:
        #preserve original
        thumb = image.copy()
        thumb.thumbnail(size,Image.LANCZOS)
        img = thumb.copy()
    img_padded = Image.new("RGBA",size)
    img_padded.paste(image,(int((size[0]-image.size[0])/2),int((size[1]-image.size[1])/2)))
    return img_padded

Or this, maybe... (forgive spaghetti)

from PIL import Image

def process_image(image, size):
    if image.size[0] > size[0] or image.size[1] > size[1]:
        #preserve original
        thumb = image.copy()
        thumb.thumbnail(size,Image.LANCZOS)
        img = thumb.copy()
    img_padded = Image.new("RGBA",size)
    img_padded.paste(image,(int((size[0]-image.size[0])/2),int((size[1]-image.size[1])/2)))
    return img_padded
从此见与不见 2024-08-11 10:33:27

你可以将 Nadia 的答案包含在这个函数中,这样你就可以控制大小和背景。

def make_square(im, min_size=36, fill_color=(255, 255, 255, 0)):
    x, y = im.size
    size = min(min_size, x, y)
    new_im = Image.new('RGBA', (size, size), fill_color)
    im.thumbnail((256, 256))
    new_im.paste(im, (int((x - size) / 2), int((y -size) / 2))
    return new_im

you can wrap Nadia's answer in this function, which gives you control over size and background.

def make_square(im, min_size=36, fill_color=(255, 255, 255, 0)):
    x, y = im.size
    size = min(min_size, x, y)
    new_im = Image.new('RGBA', (size, size), fill_color)
    im.thumbnail((256, 256))
    new_im.paste(im, (int((x - size) / 2), int((y -size) / 2))
    return new_im
浪推晚风 2024-08-11 10:33:27

为什么不简单地使用调整大小方法?

from PIL import Image
image = Image.open('/path/to/img.png')
image = image.resize((36,36), Image.LANCZOS)

请参阅本发行说明中有关调整图像大小的建议:
https://pillow.readthedocs.io/en/stable/releasenotes/ 5.3.0.html

Why not simply use the resize method ?

from PIL import Image
image = Image.open('/path/to/img.png')
image = image.resize((36,36), Image.LANCZOS)

See recommendations for image resizing in this release note:
https://pillow.readthedocs.io/en/stable/releasenotes/5.3.0.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文