从Python io.BufferedReader类的实例读取/打开图像 [英] Read/Open image from instance of Python io.BufferedReader class

查看:1750
本文介绍了从Python io.BufferedReader类的实例读取/打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从Python的io.BufferedReader类实例中正确打开TIFF图像.我使用下面的lib从GCS路径下载了该图像,但似乎无法使用传统工具打开该图像.

I'm struggling to properly open a TIFF image from an instance of Python's io.BufferedReader class. I download the image from a GCS path using the below lib, but I can't open seem to open the image with traditional tools.

# returns the <_io.BufferedReader>
file = beam.io.gcp.gcsio.GcsIO().open("<GCS_PATH>", 'r')

from PIL import Image    
img = Image.open(file.read()) <---- Fails with "TypeError: embedded NUL character"

img = Image.open(file.raw) <--- Fails when any operations are performed with "IOError(err)"

除了PIL,我对其他图书馆也开放.

I am open to other libraries besides PIL.

更新

以下内容也失败:

img = Image.open(file)

它失败并出现IOError,指出tempfile.tif: Cannot read TIFF header.

It fails with an IOError, stating tempfile.tif: Cannot read TIFF header.

推荐答案

请确保将它们都包装在ContextManager中,以便它们都可以正确关闭.

Make sure you wrap both in a ContextManager so they both get closed properly.

with beam.io.gcp.gcsio.GcsIO().open(file_path, 'r') as file, Image.open(io.BytesIO(file.read())) as multi_page_tiff:
     do_stuff()

这篇关于从Python io.BufferedReader类的实例读取/打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆