Skip to contents

Converts a PIL Image instance to a matrix.

Usage

image_to_array(img, data_format = NULL, dtype = NULL)

Arguments

img

Input PIL Image instance.

data_format

Image data format, can be either "channels_first" or "channels_last". Defaults to NULL, in which case the global setting config_image_data_format() is used (unless you changed it, it defaults to "channels_last").

dtype

Dtype to use. NULL means the global setting config_floatx() is used (unless you changed it, it defaults to "float32").

Value

A 3D array.

Example

image_path <- get_file(origin = "https://www.r-project.org/logo/Rlogo.png")
(img <- image_load(image_path))

## <PIL.Image.Image image mode=RGB size=724x561>

array <- image_to_array(img)
str(array)

##  num [1:561, 1:724, 1:3] 0 0 0 0 0 0 0 0 0 0 ...