Convert plot into pixels in R
up vote
4
down vote
favorite
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...
r png rendering rgb getpixel
add a comment |
up vote
4
down vote
favorite
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...
r png rendering rgb getpixel
1
you can use the graphics devices provided by themagick
package.
– hrbrmstr
Nov 7 at 13:18
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
1
FYI I tested withfile="clipboard"
and it's slower than both of your options.
– Moody_Mudskipper
Nov 7 at 16:05
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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...
r png rendering rgb getpixel
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...
r png rendering rgb getpixel
r png rendering rgb getpixel
edited Nov 7 at 14:15
asked Nov 7 at 12:47
Robert
688
688
1
you can use the graphics devices provided by themagick
package.
– hrbrmstr
Nov 7 at 13:18
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
1
FYI I tested withfile="clipboard"
and it's slower than both of your options.
– Moody_Mudskipper
Nov 7 at 16:05
add a comment |
1
you can use the graphics devices provided by themagick
package.
– hrbrmstr
Nov 7 at 13:18
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
1
FYI I tested withfile="clipboard"
and it's slower than both of your options.
– Moody_Mudskipper
Nov 7 at 16:05
1
1
you can use the graphics devices provided by the
magick
package.– hrbrmstr
Nov 7 at 13:18
you can use the graphics devices provided by the
magick
package.– hrbrmstr
Nov 7 at 13:18
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
1
1
FYI I tested with
file="clipboard"
and it's slower than both of your options.– Moody_Mudskipper
Nov 7 at 16:05
FYI I tested with
file="clipboard"
and it's slower than both of your options.– Moody_Mudskipper
Nov 7 at 16:05
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189750%2fconvert-plot-into-pixels-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
you can use the graphics devices provided by the
magick
package.– hrbrmstr
Nov 7 at 13:18
Looks promising - let me try...
– Robert
Nov 7 at 13:27
Given that I did everything right in using the magick package, time elapsed for extracting the information needed is not reduced (see above)...
– Robert
Nov 7 at 15:13
1
FYI I tested with
file="clipboard"
and it's slower than both of your options.– Moody_Mudskipper
Nov 7 at 16:05