Skip to contents

Write images into a TIFF file.

Usage

write_tif(
  img,
  path,
  bits_per_sample = "auto",
  compression = "none",
  overwrite = FALSE,
  msg = TRUE,
  xresolution = NULL,
  yresolution = NULL,
  resolutionunit = NULL,
  orientation = NULL,
  xposition = NULL,
  yposition = NULL,
  copyright = NULL,
  artist = NULL,
  documentname = NULL,
  datetime = NULL
)

tif_write(
  img,
  path,
  bits_per_sample = "auto",
  compression = "none",
  overwrite = FALSE,
  msg = TRUE,
  xresolution = NULL,
  yresolution = NULL,
  resolutionunit = NULL,
  orientation = NULL,
  xposition = NULL,
  yposition = NULL,
  copyright = NULL,
  artist = NULL,
  documentname = NULL,
  datetime = NULL
)

Arguments

img

An array representing the image.

  • For a multi-plane, grayscale image, use a 3-dimensional array img[y, x, plane].

  • For a multi-channel, single-plane image, use a 4-dimensional array with a redundant 4th slot img[y, x, channel, ] (see ijtiff_img 'Examples' for an example).

  • For a multi-channel, multi-plane image, use a 4-dimensional array img[y, x, channel, plane].

path

Path to the TIFF file to write to.

bits_per_sample

Number of bits per sample (numeric scalar). Supported values are 8, 16, and 32. The default "auto" automatically picks the smallest workable value based on the maximum element in img. For example, if the maximum element in img is 789, then 16-bit will be chosen because 789 is greater than 2 ^ 8 - 1 but less than or equal to 2 ^ 16 - 1.

compression

A string, the desired compression algorithm. Must be one of "none", "LZW", "PackBits", "RLE", "JPEG", "deflate" or "Zip". If you want compression but don't know which one to go for, I recommend "Zip", it gives a large file size reduction and it's lossless. Note that "deflate" and "Zip" are the same thing. Avoid using "JPEG" compression in a TIFF file if you can; I've noticed it can be buggy.

overwrite

If writing the image would overwrite a file, do you want to proceed?

msg

Print an informative message about the image being written?

xresolution

Numeric value specifying the horizontal resolution in pixels per unit. This is typically used with resolutionunit to define the physical dimensions of the image.

yresolution

Numeric value specifying the vertical resolution in pixels per unit. This is typically used with resolutionunit to define the physical dimensions of the image.

resolutionunit

Integer specifying the unit of measurement for xresolution and yresolution. Valid values are: 1 (no absolute unit), 2 (inch), or 3 (centimeter). Default is 2 (inch) if not specified.

orientation

Integer specifying the orientation of the image. Valid values are:

  • 1 = Row 0 top, column 0 left (default)

  • 2 = Row 0 top, column 0 right

  • 3 = Row 0 bottom, column 0 right

  • 4 = Row 0 bottom, column 0 left

  • 5 = Row 0 left, column 0 top

  • 6 = Row 0 right, column 0 top

  • 7 = Row 0 right, column 0 bottom

  • 8 = Row 0 left, column 0 bottom

xposition

Numeric value specifying the x position of the image in resolution units. This is typically used with resolutionunit to define the horizontal position of the image.

yposition

Numeric value specifying the y position of the image in resolution units. This is typically used with resolutionunit to define the vertical position of the image.

Character string specifying the copyright notice for the image.

artist

Character string specifying the name of the person who created the image.

documentname

Character string specifying the name of the document from which the image was scanned.

datetime

Date/time for the image. Can be provided as a character string in format "YYYY:MM:DD HH:MM:SS", a Date object, a POSIXct/POSIXlt object, or any object that can be converted to a datetime using lubridate::as_datetime(). If NULL (default), no datetime is set.

Value

The input img (invisibly).

See also

Author

Simon Urbanek wrote most of this code for the 'tiff' package. Rory Nolan lifted it from there and changed it around a bit for this 'ijtiff' package. Credit should be directed towards Lord Urbanek.

Examples

img <- read_tif(system.file("img", "Rlogo.tif", package = "ijtiff"))
#> Reading image from /usr/local/lib/R/site-library/ijtiff/img/Rlogo.tif
#> Reading an 8-bit, float image with dimensions 76x100x4x1 (y,x,channel,frame) . . .
temp_dir <- tempdir()
write_tif(img, paste0(temp_dir, "/", "Rlogo"))
#> Writing /tmp/Rtmp0SQckb/Rlogo.tif: an 8-bit, 76x100 pixel image of unsigned integer type with 4 channels and 1 frame . . .
#>  Done.
img <- matrix(1:4, nrow = 2)
write_tif(img, paste0(temp_dir, "/", "tiny2x2"))
#> Writing /tmp/Rtmp0SQckb/tiny2x2.tif: an 8-bit, 2x2 pixel image of unsigned integer type with 1 channel and 1 frame . . .
#>  Done.
list.files(temp_dir, pattern = "tif$")
#> [1] "Rlogo.tif"   "tiny2x2.tif"