ensure_grayscale: Convert 'cimg' object to grayscale via XYZ

View source: R/reliable_grayscale.R

ensure_grayscaleR Documentation

Convert 'cimg' object to grayscale via XYZ

Description

Improves on the XYZ method of grayscale by handling alpha channel and retaining relative luminance of the image

Usage

ensure_grayscale(img, bg = 1)

Arguments

img

An cimg object containing an image (from 'imager')

bg

Background color in case of alpha (default is white)

Value

An cimg object, now in grayscale (just one color channel).

Examples

# Load image using imager
pineapple_img = load.image(system.file("FallingPineapple_16x16.png", package = "ort"))

# Create grayscale version
pineapple_gray = ensure_grayscale(pineapple_img)
plot(pineapple_gray)

# Example with alpha background

alph_img = load.image(system.file("alpha_gradient_test.png", package = "ort"))

# default assumes bg = 1 (white background)
alph_gray = ensure_grayscale(alph_img)
plot(alph_gray)

alph_gray_darker = ensure_grayscale(alph_img, bg = 0.75)
plot(alph_gray_darker)


ort documentation built on Feb. 23, 2026, 5:08 p.m.