4

Does anyone know how to convert a simple plot into png_format without saving the picture...? In other words, I look for the most straight forward and fastest way to convert a simple plot into pixels, if it is not already (see code below)...

EDIT: Suggestion given by @hrbrmstr implemented...

## SolutionOne

library(png)
testONE <- system.time({
  ## Save plot as .png
  tmp <- tempfile()
  png(tmp, width = 800, height = 600, res = 72)
  plot(1:10, pch = 19, col = "yellowgreen", cex = 20)
  dev.off()
  ## Read .png
  asPixels <- readPNG(tmp)
  ## Information needed (e.g. RGB)
  dim(asPixels)
  pixONE <- asPixels[300, 400, 1:3]
})

## SolutionTwo

library(magick)
testTWO <- system.time({
  ## Produce image using graphics device
  fig <- image_graph()
  plot(1:10, pch = 19, col = "yellowgreen", cex = 20)
  ## Information needed
  pixTWO <- image_data(fig)[1:3, 400, 300]
  dev.off()
})

testONE # elapsed time: 0.064
testTWO # elapsed time: 0.164

Thanks for any hint...

Robert
  • 133
  • 8

0 Answers0