Convert plot into pixels in R











up vote
4
down vote

favorite
1












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...










share|improve this question




















  • 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















up vote
4
down vote

favorite
1












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...










share|improve this question




















  • 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













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





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...










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 14:15

























asked Nov 7 at 12:47









Robert

688




688








  • 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














  • 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








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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud