0

I am a beginner when it comes to Python and coding. I am trying to compress a 30GB TIFF stack into a smaller TIFF stack and has loseless compression. I came across a few people using the code line ZIP_DEFLATE in to do this. But, I haven't been successful in implementing such codes for compressing my TIFF files. Would you mind helping me with compiling a small code for this? Or any advice that will help with building such a code. My input raw TIFF files are single TIFF stacks that is around 30GB.

Thank you

EDIT.

Currently I stitched up a code from various sources (code below). When I run this it reads by input TIFF stack and creates an output file that is around 50% smaller. Is there anyway to compress it even more?

import os, random
import zipfile
from zipfile import ZIP_DEFLATED
from tifffile import imread, imsave
import time
import skimage
from skimage import io
from skimage.util import img_as_ubyte
from skimage import img_as_uint

    Data_folder = "/Test_Input" #@param {type:"string"}
    Result_folder = "/Test_output" #@param {type:"string"}
    
    for image in os.listdir(Data_folder):
            timelapse = imread(Data_folder+"/"+image)
            short_name = os.path.splitext(image)
            n_timepoint = timelapse.shape[0]
            Output_stack= timelapse
            for t in range(n_timepoint):
              print("Frame number: "+str(t))
              img_t = timelapse[t]
    
          
            Output_stack_16= img_as_uint(Output_stack, force_copy=False)
            os.chdir(Result_folder)
            imsave(str(short_name[0])+".tif", Output_stack_16, compress=ZIP_DEFLATED)
  • Maybe you can run `exiftool` or `tiffinfo` or `magick identify -verbose` on your image and edit the results into your question so that folks can see what you are dealing with... – Mark Setchell Feb 09 '22 at 15:47
  • Hi, Thanks for your reply. I just updated my post above with what you had asked. – Eddy_morphling Feb 09 '22 at 16:00
  • You don't seem to have added the output from any of the commands I suggested. And you have removed your `import` statements so we don't know which packages you are using... – Mark Setchell Feb 09 '22 at 16:11
  • Sorry, my bad. I have updated the import statements and the code now. – Eddy_morphling Feb 09 '22 at 16:23
  • `compress=ZIP_DEFLATED` is wrong and just happens to work by chance. Use `tifffile.imwrite(filename, data, compression='zlib')`. – cgohlke Feb 09 '22 at 18:25
  • Thanks @cgohlke. That seems to be working. I was able to compress it by 50%. I assume `zlib` is a lossless compression. Are there any other compression formats that are better than `zlib`? How can I also preserve the metadata from the original TIFF stack? Also when I compress a multi-channel z-stack the compressed TIFF stack gets saved as a single channel and the second channels gets added every other frame. Is there anyway to correct this? – Eddy_morphling Feb 10 '22 at 14:57

0 Answers0