Change color in imager and save image

33 Views Asked by At

The documentation of imager shows how to specify your own colors in a plot (see here). This is super handy but I seem to be unable to save a modified plot. How can I achieve that?

library(imager); library(dplyr)
cscale <- function(r, g, b) rgb(g, r, b)
plot(boats, colourscale=cscale, rescale=FALSE)

This works fine. But how can I save this plot? This does not work:

boats %>%
  plot(colourscale=cscale, rescale=FALSE) %>%
  save.image("pathtomachine.jpg")

Thanks.

1

There are 1 best solutions below

2
jay.sf On BEST ANSWER

Use the base pipeline of choosing a device and closing it.

> library(imager)
> cscale <- function(r, g, b) rgb(g, r, b)
> dims <- attributes(boats)$dim  ## get dims
> jpeg('foo.jpg', dims[1], dims[2])  ## open jpg device
> par(mar=c(0, 0, 0, 0))  ## switch off margins
> plot(boats, colourscale=cscale, rescale=FALSE)  ## plot
> dev.off() |> invisible()  ## close jpg device

> grep('foo.jpg', dir(), value=TRUE)  ## check if it's there
[1] "foo.jpg"

enter image description here