<<<<<<< HEAD Write image to disk — writeImage • SPATA2Write image to disk — writeImage • SPATA2
<<<<<<< HEAD

The writeImage method writes an image to a specified directory.

writeImage(object, ...)
=======
    

This function writes an image to a specified directory. Useful, if you have downloaded SPATA2 objects with SPATAData and want to make use of SPATA2's options to load/unload images that are not required at a given time.

writeImage(object, img_dir, ...)
>>>>>>> 99de33d (v3.1.0 restored 1)

# S4 method for class 'SPATA2'
writeImage(
  object,
  img_name,
<<<<<<< HEAD
  img_dir,
  overwrite = FALSE,
  transform = FALSE,
  verbose = NULL
=======
  img_dir = NULL,
  overwrite = FALSE,
  transform = FALSE,
  ...
>>>>>>> 99de33d (v3.1.0 restored 1)
)

# S4 method for class 'SpatialData'
writeImage(
  object,
  img_name,
<<<<<<< HEAD
  img_dir,
  overwrite = FALSE,
  transform = FALSE,
  verbose = TRUE
=======
  igm_dir = NULL,
  overwrite = FALSE,
  transform = FALSE,
  verbose = TRUE,
  ...
>>>>>>> 99de33d (v3.1.0 restored 1)
)

# S4 method for class 'HistoImage'
writeImage(
  object,
<<<<<<< HEAD
  img_dir,
  overwrite = FALSE,
  transform = FALSE,
  verbose = TRUE
=======
  img_dir = NULL,
  overwrite = FALSE,
  transform = FALSE,
  verbose = TRUE,
  ...
>>>>>>> 99de33d (v3.1.0 restored 1)
)

Arguments

object

An object of class SPATA2 or, in case of S4 generics, objects of classes for which a method has been defined.

<<<<<<< HEAD =======
img_dir

A character string specifying the directory where the image should be saved. If NULL, the image is written to the current image directory as obtained by getImageDir().

>>>>>>> 99de33d (v3.1.0 restored 1)
...

Additional arguments passed to EBImage::writeImage.

img_name

A character string specifying the name of the image.

<<<<<<< HEAD
img_dir

A character string specifying the directory where the image should be saved. If NULL, the image is written to the current image directory as obtained by getImageDir().

======= >>>>>>> 99de33d (v3.1.0 restored 1)
overwrite

Logical. If TRUE, existing files with the same name in the specified directory will be overwritten.

transform

Logical value. If TRUE, image transformations defined during alignImage() and/or alignImageInteractive() are applied before saving the image.

Defaults to FALSE. Only set to TRUE if you do not reassign the object after the function call. If transform is TRUE and you reassign the object, the transformed image will be saved, but the object itself will not reflect these changes (e.g., the transformation will not be undone in the object). This can lead to discrepancies between the saved image and the object’s internal state.

verbose

Logical. If TRUE, informative messages regarding the computational progress will be printed.

(Warning messages will always be printed.)

Value

As pointed out in details, this function can be used to just write an image to disk while simultaneously storing the results in the respective object. After the image is successfully written to disk, the respective object, updated in terms of image directory and resize factor, is returned invisibly. See examples.

Details

The writeImage() function writes the image associated with the specified img_name to the given directory img_dir.

Setting resize_fct to NULL:

After the image is written to the specified directory, the resize_fct transformation is set to NULL. This is to prevent an ever-decreasing reduction in image size since the factor is typically applied when the image is loaded into the object. If this factor is not reset after writing the image, subsequent loading and writing cycles would continually reduce the image size.

Differences in Assigning the Object:

The difference between using object <- writeImage(object) and simply calling writeImage(object) lies in the handling of the img_dir slot in the HistoImage class:

  • object <- writeImage(object, ...): When you assign the result of the writeImage call back to the object, the function updates the dir slot with the directory path img_dir where the image was written. This ensures that the object now knows the location of its saved image, which can be useful for tracking and future references.

  • writeImage(object, ...) without assignment: If you do not reassign the object, the image is still written to the specified directory, but the dir slot within the HistoImage object is not updated - because the updates were not reassigned.

<<<<<<< HEAD ======= >>>>>>> 99de33d (v3.1.0 restored 1)

Examples


library(SPATA2)
library(SPATAData)

object <- downloadSpataObject("UKF313T")

# contains two images
getImageNames(object)

img_name <- "hires"
img_dir <- "my/new/image_directory.png"

# Example 1: Basic usage, save the image and update the object
<<<<<<< HEAD
object <- writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE)
# The object now knows the location of the saved image.

# Example 2: Save the image without updating the object
writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE)
# The image is saved, but the object does not update its internal directory reference.
=======

old_dir <- getImageDir(object, img_name = img_name)

print(old_dir)
file.exists(old_dir) # probably FALSE

# write the image and update the object
object <- writeImage(object, img_name = img_name, img_dir = img_dir)

new_dir <- getImageDir(object, img_name = img_name)

print(new_dir)
file.exists(new_dir) # should be TRUE
identical(new_dir, img_dir) # shuold be TRUE

# Example 2: Save the image without updating the object
writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE)

>>>>>>> 99de33d (v3.1.0 restored 1)

# Example 3: Apply transformations before saving (but do not reassign the object)
writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE, transform = TRUE)
# The image is saved with the transformations applied, but since we did not reassign,
# the object does not reflect these transformations internally.

<<<<<<< HEAD
# Example 4: Apply transformations and update the object
object <- writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE, transform = TRUE)
# The image is saved with transformations applied, and the object is updated with the new directory and resize factor.
=======
# Example 4 - Pitfall: Apply transformations and update the object
object_bad <- writeImage(object, img_name = img_name, img_dir = img_dir, overwrite = TRUE, transform = TRUE)

# loading the written image again, which has been transformed during writing
# stores the already transformed image in the object
# upon extraction the same transformations are applied leading to bad alignment if any
# alignment instructions have been saved during alignImage() and/or alignImageInteractive()
object_bad <- loadImage(object)
>>>>>>> 99de33d (v3.1.0 restored 1)

Site built with pkgdown 2.1.0.