从Python实例读取/打开图像io.BufferedReader班

2024-04-25 15:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

# 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之外,我还对其他图书馆开放。在

以下也失败了:

^{pr2}$

它失败,返回IOError,声明tempfile.tif: Cannot read TIFF header.


Tags: 实例io图像imageimgreadpilwith
1条回答
网友
1楼 · 发布于 2024-04-25 15:18:12

请确保将两者都包装在ContextManager中,以便它们都能正确关闭。在

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

相关问题 更多 >