简体   繁体   English

PIL无法识别流到io.BytesIO的Google Drive图像的图像文件

[英]PIL cannot identify image file for a Google Drive image streamd into io.BytesIO

I am using the Drive API to download an image . 我正在使用Drive API下载图像 Following their file downloading documentation in Python, I end up with a variable fh that is a populated io.BytesIO instance. 根据他们在Python中的文件下载文档后,我得到了一个变量fh ,它是一个填充的io.BytesIO实例。 I try to save it as an image: 我尝试将其另存为图像:

file_id = "0BwyLGoHzn5uIOHVycFZpSEwycnViUjFYQXR5Nnp6QjBrLXJR"
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print('Download {} {}%.'.format(file['name'],
                                    int(status.progress() * 100)))
    fh.seek(0)
image = Image.open(fh) # error

The error is: cannot identify image file <_io.BytesIO object at 0x106cba890> . 错误是: cannot identify image file <_io.BytesIO object at 0x106cba890> Actually, the error does not occur with another image but is thrown with most images, including the one I linked at the beginning of this post. 实际上,该错误不会在其他图像上发生,而是在大多数图像上引发,包括我在本文开头链接的图像。

After reading this answer I change that last line to: 阅读此答案后,我将最后一行更改为:

byteImg = fh.read()
dataBytesIO = io.BytesIO(byteImg)
image = Image.open(dataBytesIO) # still the same error

I've also tried this answer , where I change the last line of my first code block to 我也尝试过这个答案 ,在这里我将第一个代码块的最后一行更改为

byteImg = fh.read()
image = Image.open(StringIO(byteImg))

But I still get a cannot identify image file <StringIO.StringIO instance at 0x106471e60> error. 但是我仍然收到一个cannot identify image file <StringIO.StringIO instance at 0x106471e60>错误。

I've tried using alternates (requests, urllib) with no fruition. 我尝试使用没有结果的替代方法(请求,urllib)。 I can Image.open the the image if I download it manually. 如果手动下载图像,则可以Image.open图像。

This error was not present a month ago, and has recently popped up into the application this code is in. I've spent days debugging this error with no success and have finally brought the issue to Stack Overflow. 这个错误一个月前没有出现,并且最近突然出现在该代码所在的应用程序中。我花了几天的时间调试此错误,但没有成功,最终将问题提交给Stack Overflow。 I am using from PIL import Image . 我正在使用from PIL import Image

Ditch the Drive service's MediaIOBaseDownload . MediaIOBaseDownload Drive服务的MediaIOBaseDownload Instead, use the webContentLink property of a media file (a link for downloading the content of the file in a browser, only available for files with binary content). 而是使用媒体文件的webContentLink属性(用于在浏览器中下载文件内容的链接,仅适用于具有二进制内容的文件)。 Read more here . 在这里阅读更多。

With that content link, we can use an alternate form of streaming—the requests and shutil libraries and the —to get the image. 通过该内容链接,我们可以使用流的另一种形式- requestsshutil库以及-获得图像。

import requests
import shutil

r = requests.get(file['webContentLink'], stream=True)
with open('output_file', 'wb') as f:
    shutil.copyfileobj(r.raw, f)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PIL 无法识别 io.BytesIO object 的图像文件 - PIL cannot identify image file for io.BytesIO object PIL.UnidentifiedImageError:无法识别图像文件 <_io.BytesIO object - PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object 引发 UnidentifiedImageError(PIL.UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x0000018CA596D350&gt; - raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000018CA596D350> Python 错误:PIL.UnidentifiedImageError:无法识别图像文件 &lt;_io.BytesIO object at 0x1144e9860&gt; - Python error: PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1144e9860> OSError:无法识别图像文件&lt;_io.BytesIO对象位于0x00000222C8A21360&gt; - OSError: cannot identify image file <_io.BytesIO object at 0x00000222C8A21360> OSError:在尝试使用 Keras 模型进行预测时,无法识别图像文件 &lt;_io.BytesIO object at ... &gt; - OSError: cannot identify image file <_io.BytesIO object at ... > when trying to predict with Keras model UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x000002154C6AE400&gt; - UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400> 具有BytesIO的PIL:无法识别图像文件 - PIL with BytesIO: cannot identify image file 枕头OSError:无法识别图像文件&lt;_io.BytesIO对象0x02345ED8&gt; - pillow OSError: cannot identify image file <_io.BytesIO object at 0x02345ED8> python PIL _io.BytesIO无法读取用画布转换的图像 - A image converted with canvas fails to be read by python PIL _io.BytesIO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM