简体   繁体   中英

Read/Open image from instance of Python io.BufferedReader class

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)"

I am open to other libraries besides PIL.

UPDATE

The following also fails:

img = Image.open(file)

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM