2

What I'm actually doing is saving images in a tiff file, using imageio.mimwrite(). But in my script, I open and close the file several times, so it erases existing images before saving news images. I want to keep existing images in the tiff file, and just add new images without erasing the previous ones. I did not find anything in the documentation which can helps me.

I'm actually using this : imageio.mimwrite("example.tiff", image, format=".tiff")

image is an array which contains arrays of integers, each array representing an image.

This code opens example.tiff, erase existing images (if they exist), and write news images. But I want to add like open("file.txt", "a") does.

Biopy
  • 167
  • 1
  • 5
  • 15
  • You want to store several images in one tiff file? – Alderven Apr 29 '19 at 10:09
  • Yes that's what I want at the end – Biopy Apr 29 '19 at 10:50
  • Try [tifffile](https://pypi.org/project/tifffile/): `tifffile.imwrite(filename, image, append=True, metadata=None)`. I would avoid this approach if possible. Opening a TIFF file and seeking to the last IFD is relatively expensive... – cgohlke Apr 30 '19 at 06:55

2 Answers2

1

I made three differently sized TIFF images with ImageMagick like this for testing:

convert -size 640x480  xc:green             green.tif
convert -size 1024x768 xc:blue              blue.tif
convert -size 400x100 gradient:cyan-yellow  gradient.tif

Then I used the tool tiffcp which is distributed with the TIFF library and the -a option to append the blue and gradient image to the green one like this:

tiffcp -a blue.tif gradient.tif green.tif

If I then check the contents of green.tiff with ImageMagick identify, I see it looks correct:

magick identify green.tif
green.tif[0] TIFF 640x480 640x480+0+0 16-bit sRGB 6.49355MiB 0.000u 0:00.000
green.tif[1] TIFF 1024x768 1024x768+0+0 16-bit sRGB 0.000u 0:00.000
green.tif[1] TIFF 400x100 400x100+0+0 16-bit sRGB 0.000u 0:00.000

And if I preview the file, all three images are there with the correct sizes and colours:

enter image description here

So, I am suggesting you consider using subprocess.run() to shell out to tiffcp.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

With tifffile writing one page at a time (in the example a CTYX multipage), you can just write straight from a n-D array if you have enough RAM for it with tifffile.imwrite(filename,array) https://pypi.org/project/tifffile/ :

import tifffile as tf
with tf.TiffWriter("filenametest.tiff",
                    #bigtiff=True,
                    #If you want to add on top of an existing tiff file (slower) uncomment below
                    #append = True,
                    imagej=False,) as tif:
    for time in range(rgb.shape[1]):
         tif.save(rgb[:,time,:,:].,
                #compress= 3,
                photometric='minisblack',
                metadata= None,
                contiguous=False,
            )
tif.close()

With python-bioformats:

https://pythonhosted.org/python-bioformats/

bioformats.write_image(pathname, pixels, pixel_type, c=0, z=0, t=0, size_c=1, size_z=1, size_t=1, channel_names=None)[source]

if you have:

  • 4 time points with

  • 3 color zstacks (11 Z's)

  • with XY at 1024 pixels. with independent numpy arrays being [3,11,1024,1024] (one for each timepoint),

  • 16bits, and named: a,b,c,d.

This should do the trick

import bioformats as bf
import numpy as np

#do something here to load a,b,c, and d

i=0
for t in [a,b,c,d]:
    for c in range(3):
        for z in range(11):
            #t here is the numpy array
            bf.write_image('/path/to/file.tiff' 
            , t
            , bf.PT_UINT16
            , c = c, z = z , t = i, size_c= 3, size_z=11, size_t=4
            )
    i+=1
João Mamede
  • 129
  • 12