减小图像的宽度/高度以适合给定的宽高比。如何? - Python图像缩略图

发布于 2024-10-12 18:55:01 字数 908 浏览 3 评论 0原文

import Image

image  = Image.open('images/original.jpg')
width  = image.size[0]
height = image.size[1]

if width > height:
    difference = width - height
    offset     = difference / 2
    resize     = (offset, 0, width - offset, height)

else:
    difference = height - width
    offset     = difference / 2
    resize     = (0, offset, width, height - offset)

thumb = image.crop(resize).resize((200, 200), Image.ANTIALIAS)
thumb.save('thumb.jpg')

这是我当前的缩略图生成脚本。它的工作原理是:

如果您有一个 400x300 的图像,而您想要一个 100x100 的缩略图,则原始图像的左侧和右侧将减少 50 个像素。因此,将其大小调整为 300x300。这使原始图像具有与新缩略图相同的纵横比。之后,它将缩小到所需的缩略图大小。

这样做的优点是:

  • 缩略图是从图像中心截取的
  • 纵横比不会搞砸

如果您将 400x300 的图像缩小到 100x100,它会看起来被压扁了。如果从 0x0 坐标获取缩略图,您将获得图像的左上角。通常,图像的焦点是中心。

我想要做的是为脚本提供任何长宽比的宽度/高度。例如,如果我想要将 400x300 图像调整为 400x100,它应该将图像的左侧和右侧削减 150px...

我想不出一种方法来做到这一点。有什么想法吗?

import Image

image  = Image.open('images/original.jpg')
width  = image.size[0]
height = image.size[1]

if width > height:
    difference = width - height
    offset     = difference / 2
    resize     = (offset, 0, width - offset, height)

else:
    difference = height - width
    offset     = difference / 2
    resize     = (0, offset, width, height - offset)

thumb = image.crop(resize).resize((200, 200), Image.ANTIALIAS)
thumb.save('thumb.jpg')

This is my current thumbnail generation script. The way it works is:

If you have an image that is 400x300 and you want a thumbnail that's 100x100, it will take 50 pixels off the left and right side of the original image. Thus, resizing it to be 300x300. This gives the original image the same aspect ratio as the new thumbnail. After that, it will shrink it down to the required thumbnail size.

The advantages of this is:

  • The thumbnail is taken from the center of the image
  • Aspect ratio doesn't get screwed up

If you were to shrink the 400x300 image down to 100x100, it will look squished. If you took the thumbnail from 0x0 coordinates, you would get the top left of the image. Usually, the focal point of the image is the center.

What I want to be able to do is give the script a width/height of any aspect ratio. For example, if I wanted the 400x300 image to be resized to 400x100, it should shave 150px off the left and right sides of the image...

I can't think of a way to do this. Any ideas?

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

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

发布评论

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

评论(1

毁梦 2024-10-19 18:55:01

您只需要比较纵横比 - 根据哪个较大,这将告诉您是否要切掉侧面或顶部和底部。例如:怎么样:

import Image

image  = Image.open('images/original.jpg')
width  = image.size[0]
height = image.size[1]

aspect = width / float(height)

ideal_width = 200
ideal_height = 200

ideal_aspect = ideal_width / float(ideal_height)

if aspect > ideal_aspect:
    # Then crop the left and right edges:
    new_width = int(ideal_aspect * height)
    offset = (width - new_width) / 2
    resize = (offset, 0, width - offset, height)
else:
    # ... crop the top and bottom:
    new_height = int(width / ideal_aspect)
    offset = (height - new_height) / 2
    resize = (0, offset, width, height - offset)

thumb = image.crop(resize).resize((ideal_width, ideal_height), Image.ANTIALIAS)
thumb.save('thumb.jpg')

You just need to compare the aspect ratios - depending on which is larger, that will tell you whether to chop off the sides or the top and bottom. e.g. how about:

import Image

image  = Image.open('images/original.jpg')
width  = image.size[0]
height = image.size[1]

aspect = width / float(height)

ideal_width = 200
ideal_height = 200

ideal_aspect = ideal_width / float(ideal_height)

if aspect > ideal_aspect:
    # Then crop the left and right edges:
    new_width = int(ideal_aspect * height)
    offset = (width - new_width) / 2
    resize = (offset, 0, width - offset, height)
else:
    # ... crop the top and bottom:
    new_height = int(width / ideal_aspect)
    offset = (height - new_height) / 2
    resize = (0, offset, width, height - offset)

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