栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将图像调整为给定边界区域的最简单方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将图像调整为给定边界区域的最简单方法是什么?

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

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+dyif __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(...)
返回的图像。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号