将图像调整为给定边界区域的最简单方法是什么? [英] What's the simplest way to resize an image to a given bounded area?

查看:77
本文介绍了将图像调整为给定边界区域的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数,例如:

I'd like to create a function, like:

def generateThumbnail(self, width, height):
     """
     Generates thumbnails for an image
     """
     im = Image.open(self._file)
     im.thumbnail((width, height), Image.ANTIALIAS)
     im.save(self._path + str(width) + 'x' + 
             str(height) + '-' + self._filename, "JPEG")

可以给文件和调整大小的地方.

Where a file can be given and resized.

当前功能可以正常工作,只是在必要时不进行裁剪.

The current function works great except it does not crop when necessary.

如果给出的是矩形图像,并且需要调整正方形大小(宽度=高度),则必须进行一些中心加权的裁剪.

In the case that a rectangular image is given, and a square resize is required (width = height), some centered-weighted cropping will have to be done.

推荐答案

在调整图像大小之前,您需要正确裁剪图像.基本思想是确定源图像的最大矩形区域,该区域的纵横比(宽高比)与缩略图图像相同,然后修剪(裁剪)其周围的任何多余部分,然后再调整为缩略图的尺寸.这是一个函数,可以计算出此类裁剪区域的大小和位置:

You need to crop the image properly before resizing it. The basic idea is to determine the largest rectangular area of the source image having the same aspect (width to height) ratio as the thumbnail image and then trim off (crop) any excess around it before resizing to the thumbnail's dimensions). Here's a function which will compute the size and location of such a cropping area:

def cropbbox(imagewidth,imageheight, thumbwidth,thumbheight):
    """ cropbbox(imagewidth,imageheight, thumbwidth,thumbheight)

        Compute a centered image crop area for making thumbnail images.
          imagewidth,imageheight are source image dimensions
          thumbwidth,thumbheight are thumbnail image dimensions

        Returns bounding box pixel coordinates of the cropping area
        in this order (left,upper, right,lower).
    """
    # determine scale factor
    fx = float(imagewidth)/thumbwidth
    fy = float(imageheight)/thumbheight
    f = fx if fx < fy else fy

    # calculate size of crop area
    cropheight,cropwidth = int(thumbheight*f),int(thumbwidth*f)

    # for centering use half the size difference of the image and the crop area
    dx = (imagewidth-cropwidth)/2
    dy = (imageheight-cropheight)/2

    # return bounding box of centered crop area on source image
    return dx,dy, cropwidth+dx,cropheight+dy


if __name__=='__main__':

    print("===")
    bbox = cropbbox(1024,768, 128,128)
    print("cropbbox(1024,768, 128,128): {}".format(bbox))

    print("===")
    bbox = cropbbox(768,1024, 128,128)
    print("cropbbox(768,1024, 128,128): {}".format(bbox))

    print("===")
    bbox = cropbbox(1024,1024, 96,128)
    print("cropbbox(1024,1024, 96,128): {}".format(bbox))

    print("===")
    bbox = cropbbox(1024,1024, 128,96)
    print("cropbbox(1024,1024, 128,96): {}".format(bbox))

确定裁剪区域后,调用im.crop(bbox),然后在返回的图像上调用im.thumbnail(...).

After determining the crop area, call im.crop(bbox) and then call im.thumbnail(...) on the image returned.

这篇关于将图像调整为给定边界区域的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆