apply_fun_to_imgs: Apply a function to images

View source: R/apply_fun_to_imgs.R

apply_fun_to_imgsR Documentation

Apply a function to images

Description

Most of the functions in pliman can be applied to a list of images, but this can be not ideal to deal with lots of images, mainly if they have a high resolution. For curiosity, a 6000 x 4000 image use nearly 570 Megabytes of RAM. So, it would be impossible to deal with lots of images within R. apply_fun_to_img() applies a function to images stored in a given directory as follows:

  • Create a vector of image names that contain a given pattern of name.

  • Import each image of such a list.

  • Apply a function to the imported image.

  • Export the mutated image to the computer.

If parallel is set to FALSE (default), the images are processed sequentially, which means that one image needs to be imported, processed, and exported so that the other image can be processed. If parallel is set to TRUE, the images are processed asynchronously (in parallel) in separate R sessions (3) running in the background on the same machine. It may speed up the processing time when lots of images need to be processed.

Usage

apply_fun_to_imgs(
  pattern,
  fun,
  ...,
  dir_original = NULL,
  dir_processed = NULL,
  prefix = "",
  suffix = "",
  parallel = FALSE,
  workers = 3,
  verbose = TRUE
)

Arguments

pattern

A pattern to match the images' names.

fun

A function to apply to the images.

...

Arguments passed on to fun.

dir_original, dir_processed

The directory containing the original and processed images. Defaults to NULL, which means that the current working directory will be considered. The processed image will overwrite the original image unless a prefix/suffix be used or a subfolder is informed in dir_processed argument.

prefix, suffix

A prefix and/or suffix to be included in the name of processed images. Defaults to "".

parallel

If TRUE processes the images asynchronously (in parallel) in separate R sessions (3 by default) running in the background on the same machine. It may speed up the processing time, especially when pattern is used is informed.

workers

A positive numeric scalar or a function specifying the number of parallel processes that can be active at the same time. Defaults to 3.

verbose

Shows the progress in console? Defaults to TRUE.

Value

Nothing. The processed images are saved to the current working directory.

Examples

# apply_fun_to_imgs("pattern", image_resize, rel_size = 50)

pliman documentation built on Oct. 15, 2023, 1:06 a.m.