# Reading individual slices of a TIFF stack (aka multipage TIFF)

**URL:** https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772
**Category:** Visualization
**Tags:** images
**Created:** [August 1, 2018, 12:34am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772 "2018-08-01T00:34:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tross](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tross](https://discourse.julialang.org/u/tross)
#### Post date: [August 1, 2018, 12:34am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/1 "2018-08-01T00:34:24Z")

</div>

Hello,

I am having trouble finding a way to access individual slices of a TIFF stack. I have a bunch of large TIFF stacks that are all too big to load into memory. In Matlab you can load a specific index of a TIFF stack through `imread(filename,index).` With Images.jl an entire image stack can be read into memory with the `load()` function. I have tried using `load("image.tif";view=true)` but it still appears to be reading the whole stack into memory (actually adding in the keyword ` view=true` makes things even worse. It took up ~12GB of memory while reading in 2GB file).

If anyone can provide advice I would quite appreciate it.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 1, 2018, 3:20am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/2 "2018-08-01T03:20:36Z")

</div>

If you can’t find a nice Julia solution, perhaps you could just split your TIFF stack into individual image files and then load the appropriate file corresponding to the image you want? Something like: [imagemagick - Creating and splitting large multipage TIFF images - Super User](https://superuser.com/a/1158133)

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [August 1, 2018, 2:43pm UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/3 "2018-08-01T14:43:06Z")

</div>

You can do that with GMT.jl but unfortunately we added an unnoticed bug in GMT5 (the C library, not the Julia wrapper) that is fixed only on the GMT trunk version. So access that facility you will need to have to build GMT dev version yourself.  
GDAL.jl is probably another option.

---

<div class="post-metadata">

### Author: ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)
#### Post date: [August 1, 2018, 6:37pm UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/4 "2018-08-01T18:37:53Z")

</div>

`Images.jl` certainly supports the ability to do out-of-memory loading, but it’s highly dependent on the file reader. For example, my package for loading OME-TIFFs (Multipage TIFFs) **does not** support [out-of-memory loading](https://github.com/tlnagy/OMETIFF.jl/issues/3) while [NRRD.jl **does**](https://github.com/JuliaIO/NRRD.jl)

TL;DR: IMO the best solution is to use NRRD files for now with Images.jl

---

<div class="post-metadata">

### Author: ![tross](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tross](https://discourse.julialang.org/u/tross)
#### Post date: [August 2, 2018, 1:18am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/5 "2018-08-02T01:18:20Z")

</div>

Thank you for the replies!

> [@joa-quim](#):
>
> You can do that with GMT.jl but unfortunately we added an unnoticed bug in GMT5 (the C library, not the Julia wrapper) that is fixed only on the GMT trunk version. So access that facility you will need to have to build GMT dev version yourself.  
> GDAL.jl is probably another option.

Interesting, I am having trouble finding documentation for loading in tiffs with either of the packages. Could you point me to where this information is/may be?

> [@tlnagy](#):
>
> `Images.jl` certainly supports the ability to do out-of-memory loading, but it’s highly dependent on the file reader. For example, my package for loading OME-TIFFs (Multipage TIFFs) **does not** support [out-of-memory loading](https://github.com/tlnagy/OMETIFF.jl/issues/3) while [NRRD.jl **does**](https://github.com/JuliaIO/NRRD.jl)
> 
> TL;DR: IMO the best solution is to use NRRD files for now with Images.jl

That is quite unfortunate that the package doesn’t support out-of-memory loading. Due to the number/size of the files and the metadata they contain is important for downstream applications, I would strongly prefer avoiding splitting up or converting the files. Would it be feasible for me to use functions in your package to implement a solution similar to looping with PIL’s [seek function](https://stackoverflow.com/questions/18602525/python-pil-for-loop-to-work-with-multi-image-tiff)?

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [August 2, 2018, 9:24am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/6 "2018-08-02T09:24:00Z")

</div>

Sorry, new modules addded recently and docs not fully updated (and I’m in vacations now).  
There is a new module now called _gmtread_ that lets do what you want and has an on line help. Basically you do

I = gmtread(“fname.tif”, img=true, band=0);

To read the first band, and so on for the other bands. As I said before, this needs GMT6dev to work and there is another thing you must do. Edit the GMT.jl file and change _GMTver_ from 5 to 6.

If you are on Windows you can use the GMT binaries from [here](http://w3.ualg.pt/~jluis/mirone/downloads/gmt.html)

---

<div class="post-metadata">

### Author: ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)
#### Post date: [August 2, 2018, 8:49pm UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/7 "2018-08-02T20:49:02Z")

</div>

> [@tross](#):
>
> That is quite unfortunate that the package doesn’t support out-of-memory loading. Due to the number/size of the files and the metadata they contain is important for downstream applications, I would strongly prefer avoiding splitting up or converting the files. Would it be feasible for me to use functions in your package to implement a solution similar to looping with PIL’s [seek function](https://stackoverflow.com/questions/18602525/python-pil-for-loop-to-work-with-multi-image-tiff)?

To answer your question: Yes, you should be able to use the functions from my package to do that. I currently load all slices inside a multipage TIFF [here](https://github.com/tlnagy/OMETIFF.jl/blob/0524edfb1d5ae4408be5f033be9cb9fd5e91ed78/src/loader.jl#L63-L67). You could modify that to only load a specific slice.

More in general: I haven’t had time to work out the details for memory mapping multipage OME-TIFF files, but I am [working on it](https://github.com/tlnagy/OMETIFF.jl/issues/3). The main rub is that I need to create a performant way of accessing arbitrary slices through the data. Your case isn’t too hard since multipage TIFFs store slices in a XY format so it’s fast to access that. Just get the slice’s location and load that single one in. However, what about if the user wants XZ or ZT or CZY? That’s when it gets a little hairy. Julia’s standard `mmap` doesn’t work well for discontiguous data so I’ll need to write a custom Array type to do this quickly (@tim.holy suggested this [here](https://github.com/JuliaImages/Images.jl/issues/587#issuecomment-278844106)).

The problem is mostly finding the time to work this out and make it fast!

---

<div class="post-metadata">

### Author: ![tross](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tross](https://discourse.julialang.org/u/tross)
#### Post date: [August 15, 2018, 2:22am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/8 "2018-08-15T02:22:54Z")

</div>

For anyone who may run into this problem in the future here is the most straight forward solution I’ve been able to figure out in Julia. Due to the one or more conversion steps, it probably isn’t the fastest solution, but it doesn’t require modifying packages:

```julia
using ImageMagick

sliceindex = 2 #careful to note ImageMagick's starting index is 0 
cimgslice = ImageMagick.load(string("imagefile.tif",sliceindex)) #loads slice as a ColorType array
nimgslice = float.(cimgslice) #convert slice to a number array. 

#Note, the values are normalized to a range of [0 1] with 1 being the maximum value of the image's bit depth. 
imgslice = trunc.(Int,nimgslice.*(2^16-1))#Example of converting to the actual pixel intensity values for a 16 bit gray scale image.

```

---

<div class="post-metadata">

### Author: ![JiangXL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiangxl/32/13720_2.png) [@JiangXL](https://discourse.julialang.org/u/JiangXL)
#### Post date: [October 16, 2020, 2:50am UTC](https://discourse.julialang.org/t/reading-individual-slices-of-a-tiff-stack-aka-multipage-tiff/12772/9 "2020-10-16T02:50:14Z")

</div>

> [@tross](#):
>
> ```julia
> cimgslice = ImageMagick.load(string("imagefile.tif",sliceindex)) #loads slice as a ColorType array
> 
> ```

`cimgslice = ImageMagick.load(string("imagefile.tif",[1]))`  
works for me rather than  
`cimgslice = ImageMagick.load(string("imagefile.tif",1))`

Julia 1.5 with ImageMagick v1.1.6
