Learn R Programming

spectrakit (version 0.1.2)

makeComposite: Create a labeled image grid

Description

Generates a composite image grid with customizable layout, labels and resizing options. Suitable for spectra and other image types.

Usage

makeComposite(
  folder = ".",
  custom_order = NULL,
  rows = NULL,
  cols = NULL,
  spacing = 15,
  resize_mode = c("none", "fit", "fill", "width", "height", "both"),
  labels = list(),
  label_settings = list(),
  background_color = "white",
  desired_width = 15,
  width_unit = "cm",
  ppi = 300,
  output_format = "tiff",
  output_folder = NULL
)

Value

Saves image composite to a specified output folder. Returns `NULL` (used for side-effects).

Arguments

folder

Character. Path to the folder containing images. Default is working directory (`"."`).

custom_order

Character vector. Set of filenames (use NA for blank slots).

rows

Integer. Number of rows in the grid.

cols

Integer. Number of columns in the grid.

spacing

Integer. Spacing (in pixels) between tiles. Default is `15`

resize_mode

Character. Method to resize panels in the composite. Options are:

`"none"`

Keep each panel at its original size.

`"fit"`

Scale each panel to fit within the smallest image dimensions, preserving aspect ratio.

`"fill"`

Scale and crop each panel to exactly fill the smallest dimensions.

`"width"`

Resize each panel to the minimum width, keeping the original height.

`"height"`

Resize each panel to the minimum height, keeping the original width.

`"both"`

Force panels to the exact width and height, which may distort aspect ratio.

labels

List of up to 4 character vectors. Each vector corresponds to one label layer and must be the same length as the number of non-NA images. Use empty strings "" or NULL entries to omit specific labels.

label_settings

List of named lists. Each named list specifies settings for a label layer. Options include:

`size`

Font size (e.g., 100).

`color`

Font color.

`font`

Font family (e.g., `"Arial"`).

`boxcolor`

Background color behind text, or `NA` for none.

`location`

Offset from the gravity anchor (e.g., `"+10+10"`).

`gravity`

Placement anchor for the label (e.g., `"northwest"`).

`weight`

Font weight (e.g., `400` = normal, `700` = bold).

background_color

Character. Background color used for blank tiles and borders. Use `"none"` for transparency. Default is `"white"`.

desired_width

Numeric. Desired width of final image (in cm or px). Default is `15`

width_unit

Character. Either "cm" or "px". Default is `"cm"`

ppi

Numeric. Resolution (pixels per inch) for output file. Default is `300`

output_format

Character. File format for saving plots. Examples: `"tiff"`, `"png"`, `"pdf"`. Default is `"tiff"`.

output_folder

Character. Path to folder where image is saved. If NULL (default), image is not saved; if `"."`, image is saved in the working directory.

Examples

Run this code
library(magick)

tmp_dir <- file.path(tempdir(), "spectrakit_imgs")
dir.create(tmp_dir, showWarnings = FALSE)

# Create and save img1
img1 <- image_blank(100, 100, "white")
img1 <- image_draw(img1)
symbols(50, 50, circles = 30, inches = FALSE, add = TRUE, bg = "red")
dev.off()
img1_path <- file.path(tmp_dir, "img1.png")
image_write(img1, img1_path)

# Create and save img2
img2 <- image_blank(100, 100, "white")
img2 <- image_draw(img2)
rect(20, 20, 80, 80, col = "blue", border = NA)
dev.off()
img2_path <- file.path(tmp_dir, "img2.png")
image_write(img2, img2_path)

# Create composite
makeComposite(
        folder = tmp_dir,
        custom_order = c("img1.png", "img2.png"),
        rows = 1,
        cols = 2,
        labels = list(c("Red Circle", "Blue Rectangle")),
        label_settings = list(
                list(size = 5, font = "Arial", color = "black", boxcolor = "white",
                     gravity = "northwest", location = "+10+10", weight = 400)
        ),
        resize_mode = "none",
        desired_width = 10,
        width_unit = "cm",
        ppi = 300,
        output_format = "png",
        output_folder = tmp_dir
)

Run the code above in your browser using DataLab