图像大小调整中的逻辑错误
Logical error in image resizing
我正在使用以下代码,它负责使用 python 的枕头库更改图像分辨率。我正在 aws lambda 上部署代码。代码运行良好,但在调整大小时存在一个简单的逻辑缺陷。该代码旨在按如下方式工作:
1.上传图片到S3 bucket
2.代码将图像调整为2个不同的尺寸并将其上传到不同的存储桶。
但是代码以相同的尺寸上传了 2 张图片,我无法理解为什么
from __future__ import print_function
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image
s3_client = boto3.client('s3')
def resize_image_DOG(image_path, resized_path):
with Image.open(image_path) as image:
dim=(3840,2160)
image.resize(dim)
image.save(resized_path)
def resize_image_CAT(image_path, resized_path):
with Image.open(image_path) as image:
dim=(1920,1080)
image.resize(dim)
image.save(resized_path)
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
upload_path_1 = '/tmp/resized-{}{}'.format('test1',key)
upload_path_2 = '/tmp/resized-{}{}'.format('test2',key)
s3_client.download_file(bucket, key, download_path)
resize_image_DOG(download_path, upload_path_1)
resize_image_CAT(download_path, upload_path_2)
s3_client.upload_file(upload_path_1, '{}resized'.format(bucket), key+'xxxhdpi-3840*2160'+'.png')
s3_client.upload_file(upload_path_2, '{}resized'.format(bucket), key+'xxxhdpi-1920*1080'+'.png')
上传到resized bucket中的2张resized images尺寸相同,即第一个resize_image_DOG尺寸(3340x2160)只有,但名称正确,请帮助
我查看了 pillow 文档:image.resize()
returns 调整大小的图像,并没有改变传递的图像。
所以应该是
image = image.resize(dim)
我正在使用以下代码,它负责使用 python 的枕头库更改图像分辨率。我正在 aws lambda 上部署代码。代码运行良好,但在调整大小时存在一个简单的逻辑缺陷。该代码旨在按如下方式工作: 1.上传图片到S3 bucket 2.代码将图像调整为2个不同的尺寸并将其上传到不同的存储桶。
但是代码以相同的尺寸上传了 2 张图片,我无法理解为什么
from __future__ import print_function
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image
s3_client = boto3.client('s3')
def resize_image_DOG(image_path, resized_path):
with Image.open(image_path) as image:
dim=(3840,2160)
image.resize(dim)
image.save(resized_path)
def resize_image_CAT(image_path, resized_path):
with Image.open(image_path) as image:
dim=(1920,1080)
image.resize(dim)
image.save(resized_path)
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
upload_path_1 = '/tmp/resized-{}{}'.format('test1',key)
upload_path_2 = '/tmp/resized-{}{}'.format('test2',key)
s3_client.download_file(bucket, key, download_path)
resize_image_DOG(download_path, upload_path_1)
resize_image_CAT(download_path, upload_path_2)
s3_client.upload_file(upload_path_1, '{}resized'.format(bucket), key+'xxxhdpi-3840*2160'+'.png')
s3_client.upload_file(upload_path_2, '{}resized'.format(bucket), key+'xxxhdpi-1920*1080'+'.png')
上传到resized bucket中的2张resized images尺寸相同,即第一个resize_image_DOG尺寸(3340x2160)只有,但名称正确,请帮助
我查看了 pillow 文档:image.resize()
returns 调整大小的图像,并没有改变传递的图像。
所以应该是
image = image.resize(dim)