如何让 PIL 在创建缩略图时考虑最短边?

发布于 2024-10-05 21:17:09 字数 855 浏览 3 评论 0原文

目前,对于我的下面的功能...图像将根据最长边调整大小。

基本上图像的高度较大,高度为 200px。宽度将只是...无论如何...

如果图像具有较大的宽度,则宽度将为 200px,并且高度将相应调整。

我该如何扭转这个局面!?我希望这个函数考虑到最短边。

我这个函数写错了吗?

def create_thumbnail(f, width=200, height=None, pad = False):
    #resizes the longest side!!! doesn't even care about the shortest side
    #this function maintains aspect ratio.
    if height==None: height=width
    im = Image.open(StringIO(f))
    imagex = int(im.size[0])
    imagey = int(im.size[1])
    if imagex < width or imagey < height:
        pass
        #return None
    if im.mode not in ('L', 'RGB', 'RGBA'):
        im = im.convert('RGB')
    im.thumbnail((width, height), Image.ANTIALIAS)
    thumbnail_file = StringIO()
    im.save(thumbnail_file, 'JPEG')
    thumbnail_file.seek(0)
    return thumbnail_file

Currently, for my function below...the image will be resized based on the longest side.

Basically the image has a larger height, the height will be 200px. The width will be just...whatever...

If the image has a larger width, the width will be 200px, and the height will adjust accordingly.

How do I flip this around!? I want this function to take into account the shortest side.

Am I writing this function incorrectly??

def create_thumbnail(f, width=200, height=None, pad = False):
    #resizes the longest side!!! doesn't even care about the shortest side
    #this function maintains aspect ratio.
    if height==None: height=width
    im = Image.open(StringIO(f))
    imagex = int(im.size[0])
    imagey = int(im.size[1])
    if imagex < width or imagey < height:
        pass
        #return None
    if im.mode not in ('L', 'RGB', 'RGBA'):
        im = im.convert('RGB')
    im.thumbnail((width, height), Image.ANTIALIAS)
    thumbnail_file = StringIO()
    im.save(thumbnail_file, 'JPEG')
    thumbnail_file.seek(0)
    return thumbnail_file

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

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

发布评论

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

评论(2

云裳 2024-10-12 21:17:09

使用调整大小而不是缩略图

缩略图背后的要点是可以轻松地缩小图像以适应特定的边界框,并保持纵横比。这意味着如果您的边界框是正方形,则图像的较长边决定所使用的比例。

resize 为您提供更直接的控制 - 您可以准确指定所需的大小。

实际上,由于您想保留方面,您仍然可以使用缩略图,但您需要捏造边界框。在调用thumbnail之前,请尝试执行以下操作:

old_aspect = float(imagex)/float(imagey)
new_aspect = float(width)/float(height)
if old_aspect < new_aspect:
  height = int(width / old_aspect)
else:
  width = int(height * old_aspect)

Use resize instead of thumbnail.

The point behind thumbnail is to make it easy to scale an image down to fit within a particular bounding box preserving aspect ratio. That means that if your bounding box is a square, the longer side of the image determines the scale used.

resize gives you more direct control -- you specify exactly what size you want.

Actually, since you want to preserve aspect you could still use thumbnail, but you need to fudge the bounding box. Before your call to thumbnail, try doing this:

old_aspect = float(imagex)/float(imagey)
new_aspect = float(width)/float(height)
if old_aspect < new_aspect:
  height = int(width / old_aspect)
else:
  width = int(height * old_aspect)
薄情伤 2024-10-12 21:17:09

我不久前将这个函数放在一起:

def thumbnail(img, size=150):

    from math import floor
    from PIL import Image

    img = img.copy()

    if img.mode not in ('L', 'RGB'):
        img = img.convert('RGB')

    width, height = img.size

    if width == height:
        img.thumbnail((size, size), Image.ANTIALIAS)

    elif height > width:
        ratio = float(width) / float(height)
        newwidth = ratio * size
        img = img.resize((int(floor(newwidth)), size), Image.ANTIALIAS)

    elif width > height:
        ratio = float(height) / float(width)
        newheight = ratio * size
        img = img.resize((size, int(floor(newheight))), Image.ANTIALIAS)

    return img

image = thumbnail(image, size=600)

它将图像的大小调整为最大尺寸,并保留其最长的尺寸并保留纵横比

i put together this function a while ago:

def thumbnail(img, size=150):

    from math import floor
    from PIL import Image

    img = img.copy()

    if img.mode not in ('L', 'RGB'):
        img = img.convert('RGB')

    width, height = img.size

    if width == height:
        img.thumbnail((size, size), Image.ANTIALIAS)

    elif height > width:
        ratio = float(width) / float(height)
        newwidth = ratio * size
        img = img.resize((int(floor(newwidth)), size), Image.ANTIALIAS)

    elif width > height:
        ratio = float(height) / float(width)
        newheight = ratio * size
        img = img.resize((size, int(floor(newheight))), Image.ANTIALIAS)

    return img

image = thumbnail(image, size=600)

it resizes an image down to at most size on its longest size preserving aspect ratio

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