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("", 'r')
from PIL import Image
img = Image.open(file.read())
img = Image.open(file.raw)
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()