How to download and display image from url in R?

My goal is to download the image from the url and then display it in R.

I got the url and figured out how to download it. But the downloaded file cannot be viewed because it is "damaged, damaged or too large."

y = "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"
download.file(y, 'y.jpg')

I also tried

image('y.jpg')

in R, but the error message displays as:

Error in image.default("y.jpg") : argument must be matrix-like 

Any suggestions?

+9
source share
2 answers

If I try your code, it is like loading an image. However, when it opens using the Windows image viewer, it also says that it is damaged. The reason for this is because you did not specify modein the instructions download.file.

:

download.file(y,'y.jpg', mode = 'wb')

. ?download.file

, , , .

R,

jj <- readJPEG("y.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)

.jpeg R 2.15   R 3.1.0

+21

library("jpeg")

library("png")

x <- "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"    

image_name<- readJPEG(getURLContent(x)) # for jpg

image_name<- readPNG(getURLContent(x)) # for png
-1

All Articles