当图像到达Dropbox并上载到Azure Storage时,我需要下载该图像。我使用Kubeless无服务器函数来完成此操作,该函数由Dropbox的推送通知触发。这个下载和上传工作正常。但是,我想要访问图像的exif数据,这样我就可以将关于图像的元数据发送到RabbitMQ队列。当我尝试打开要与Python的Pillow或exif模块一起使用的图像时,我得到了这个错误:'UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte'
dbx = dropbox.Dropbox(os.environ['DROPBOX_TOKEN'])
for entry in dbx.files_list_folder('').entries:
print('File found: ' + entry.name)
md, response = dbx.files_download('/' + entry.name)
file_stream = response.content
# Code to upload to Azure Storage and delete file from Dropbox here我试过了:
with open(file_stream, 'rb') as data:
# do things with file和使用枕头:
image = Image.open(file_stream)
image.show()并且每次都会得到相同的错误。我刚开始使用Python并以这种方式处理文件,因此如果有任何帮助,我将不胜感激。
发布于 2021-03-05 16:04:47
我正在尝试打开文件,而file_stream已经是打开的文件内容。
Python请求文档提供了一个以适合我方式使用Pillow的Image.open()的示例:
>>> from PIL import Image
>>> from io import BytesIO
>>> i = Image.open(BytesIO(r.content))https://2.python-requests.org/en/master/user/quickstart/#binary-response-content
为了便于将来参考,这种打开图像的方式允许我成功地访问exif数据(img_exif = image.getexif()),而在使用Image.fromBytes()时exif数据是空的。我不知道为什么。
https://stackoverflow.com/questions/66459153
复制相似问题