dahtah / imager

R package for image processing
GNU Lesser General Public License v3.0
187 stars 43 forks source link

[Suggestion] Adding reverse of 'magick2cimg' function #99

Closed ShotaOchi closed 5 years ago

ShotaOchi commented 5 years ago

I suggest adding new function that is a reverse of 'magick2cimg' function.

magick package has functions that are not implemented in imager. I use the functions. I think it would be convenient if we can convert imager object into magick object easily.

An example of the new function is shown below.

cimg2magick <- function(im, format = "png") 
{
  tmp <- tempfile(fileext = paste(".", format, sep = ""))
  on.exit(unlink(tmp))
  save.image(im, tmp)
  magick::image_read(tmp)
}

What do you think?

dahtah commented 5 years ago

Thanks Shota, I agree with the suggestion. I think there ought to be a better way of doing the conversion than writing to disk, though. I don't know magick at all but there has to be a function for constructing a magick object from RGB data in memory, right?

ShotaOchi commented 5 years ago

You're right. A better example is shown below.

cimg2magick <- function(im) 
{
  image_read(as.raster(im))
}
dahtah commented 5 years ago

That's much better, but there's still an inefficiency and a possible loss of information if you have "raster" as an intermediate format (it converts to an array of RGB values represented as a character string). There has to be a more direct way. Happy to accept this in a pull request if you make one.