相关疑难解决方法(0)

AWS S3 图像保存丢失元数据

我正在使用用 python 2.7x 编写的 AWS Lambda 函数,该函数下载、保存到 /tmp ,然后将图像文件上传回存储桶。

我的图像元数据从原始存储桶开始,带有 http 标头,如 Content-Type= image/jpeg 等。

使用 PIL 保存图像后,所有标题都消失了,只剩下 Content-Type = binary/octet-stream

据我所知,由于 PIL 的工作方式, image.save 正在丢失标题。如何保留元数据或至少将其应用于新保存的图像?

我看过帖子暗示这个元数据在 exif 中,但我试图从原始文件中获取 exif 信息并应用到保存的文件中,但没有运气。反正我不清楚它在exif数据中。

部分代码给出了我在做什么的想法:

def resize_image(image_path):
    with Image.open(image_path) as image:
    image.save(upload_path, optimize=True)

def handler(event, context):
    global upload_path
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode("utf8"))

        download_path = '/tmp/{}{}'.format(uuid.uuid4(), file_name)
        upload_path = '/tmp/resized-{}'.format(file_name)

        s3_client.download_file(bucket, key, download_path)

        resize_image(download_path)
        s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)
Run Code Online (Sandbox Code Playgroud)

感谢 Sergey,我改为使用 get_object 但响应缺少元数据:

response = s3_client.get_object(Bucket=bucket,Key=key)
Run Code Online (Sandbox Code Playgroud)

response= {u'Body': , u'AcceptRanges': …

python amazon-s3 python-imaging-library

2
推荐指数
1
解决办法
3336
查看次数

标签 统计

amazon-s3 ×1

python ×1

python-imaging-library ×1