Write numpy array as tiff file and compress it using gzip

150 Views Asked by At

I want to write a numpy array (Dim - 3, shape - (2400, 2400, 3), Type - float32) to a tiff file and then compress it to gz file without needing a buffer.

from PIL import Image
with mgzip.open(f'{file_path}.gz', 'wb') as f_out:
  byte_stream = io.BytesIO()
  tiff_img = Image.fromarray(img_array, mode='F')
  tiff_img.save(byte_stream, "TIFF")
  f_out.write(byte_stream.getbuffer())

The above code saves img_array as tiff file to a buffer and then compresses it as .gz file. So is there a way to do it without using the "byte-stream"? (Open to using opencv also)

Just to add context: I am hoping to improve performance by avoiding byte_stream. Just wanted something like "tiff_img.save(f_out, "TIFF")" i.e making tiff_img write directly to f_out. But the prev line is not working i.e the tiff file is not being compressed when I write directly to the f_out

0

There are 0 best solutions below