0

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

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
  • What is your issue with using `BytesIO()`? With opencv you can use the `imencode` function as described [here](https://stackoverflow.com/a/52865864/18667225). But the rest will be similar. – Markus Apr 20 '23 at 06:10
  • There's no context given in your question as to what you are trying to achieve, or why, but this may be relevant https://kokoalberti.com/articles/geotiff-compression-optimization-guide/ – Mark Setchell Apr 20 '23 at 06:45

0 Answers0