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

时间:2020-08-12 08:41:30

标签: python python-imaging-library

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

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

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

    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)

1 个答案:

答案 0 :(得分:2)

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

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, new_height), Image.ANTIALIAS)

或者将它们放在一起:

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