Extracting tiff-layers

Last updated on 2026-06-15 | Edit this page

Estimated time: 0 minutes

Overview

Questions

  • How do I extract layers from a tiff-file?

Objectives

  • Demonstrate how to extract layers from tiff-files
Key Points
  • Layered tiff-files can contain different information

Tiff (sometimes tif) is a lossless image file format for storing raster graphics. Originally it was designed as a common format for manufacturers of scanners. Currently it is popular among photographers as an alternative to lossy formats like jpg and png. From the start it allowed for storing several images in one file, eg scans in different resolutions or multiple pages of a multi-page document.

What is relevant here, is the application of storing different images of the same object in the same file. It is a popular format for storing satelite imagery, where each layer contains different exposures of the same geographical area. Layers can be split by color, where a layer containing the green channel of an rgb-image, can make it easier to identify vegetation.

Layers can also contain different spectral channels, eg infrared in one channel and visible light in another.

All this makes it relevant to be able to split a tiff-file in its individual layers. Several applications supports this (and other manipulation of the files):

One library for Python supporting this is Pillow

In R we use the library raster:

R

library(raster)

OUTPUT

Loading required package: sp

Here is an example of a layered tif:

Femlagstiff
Femlagstiff

We use the stack function to read the file:

R

tif <- stack("episodes/fig/poppendorf_5_band.tif")

WARNING

Warning: episodes/fig/poppendorf_5_band.tif: No such file or directory (GDAL
error 4)

ERROR

Error in `.rasterObjectFromFile()`:
! Cannot create a RasterLayer object from this file. (file does not exist)

This specific file contains 5 layers:

R

raster::nlayers(tif)

ERROR

Error in `h()`:
! error in evaluating the argument 'x' in selecting a method for function 'nlayers': object 'tif' not found

Each layer have name:

R

names(tif)

ERROR

Error:
! object 'tif' not found

In some tiff-files they do not have names, but numbers.

We can plot the layers individually:

R

plot(tif$poppendorf_5_band_1)

ERROR

Error in `h()`:
! error in evaluating the argument 'x' in selecting a method for function 'plot': object 'tif' not found

And we can save the individual layers in separate files:

R

writeRaster(tif$poppendorf_5_band_1, "filename.tif")

Note


This file is taken from the library plainview. More specifically it is part of a collections of sattelite images from Landsat 8, of the village Poppendorf, located a bit to the right of Rostock in Germany.

The individual layers are images captured in different parts of the electromagnetic spectrum, both visible light and near infra red.

Original Landsat images can be accessed from USGS. Note that this example is colored with false colors, and stacked into a single file. Most data from USGS are provided in individual images for each “band” of the spectrum.

Please remember that not all TIFF-files have multiple layers. If you can only extract a single layer, it might be because there is only one layer in it.