调整枕头尺寸的图像-保持宽高比

问题描述

我有以下示例,我希望使用枕头来调整图像的大小。

您可以看到用户传递了一个新的宽度,但是没有高度。在保持相同的宽高比的同时,我如何计算出该图像的新高度?

图像不是正方形,而是矩形,因此高度和宽度不会相同。

    orig_image = Image.open(get_full_path(file.relative_path))
    new_width = int(request.args.get('w'))

    # TODO resize properly,we need to work out new image height
    resized_image = orig_image.resize((new_width,),Image.ANTIALIAS)

解决方法

听起来您想像这样从原始图像中获取原始长宽比:

new_width

一旦收到new_height,您就可以像这样计算new_height = new_width * aspect_ratio

resized_image = orig_image.resize((new_width,new_height),Image.ANTIALIAS)

然后您可以正确调整图像大小:

orig_image = Image.open(get_full_path(file.relative_path))
aspect_ratio = orgin_image.width / organ_image.height

new_width = int(request.args.get('w'))
new_height = new_width * aspect_ratio

resized_image = orig_image.resize((new_width,Image.ANTIALIAS)

或者将它们放在一起:

aws athena get-query-execution --region AWS_REGION --query-execution-id ID

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...