如何从 R 中的 URL 下载和显示图像? [英] how to download and display an image from an URL in R?
问题描述
我的目标是从 URL 下载图像,然后在 R 中显示.
My goal is to download an image from an URL and then display it in R.
我得到了一个 URL 并想出了如何下载它.但无法预览下载的文件,因为它已损坏、损坏或太大".
I got an URL and figured out how to download it. But the downloaded file can't be previewed because it is 'damaged, corrupted, or is too big'.
y = "http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg"
download.file(y, 'y.jpg')
我也试过
image('y.jpg')
在 R 中,但错误消息显示如下:
in R, but the error message shows like:
Error in image.default("y.jpg") : argument must be matrix-like
有什么建议吗?
推荐答案
如果我尝试您的代码,它看起来像已下载图像.但是,当用 Windows 图像查看器打开时,它也说它已损坏.原因是您没有在 download.file
语句中指定 mode
.
If I try your code it looks like the image is downloaded. However, when opened with windows image viewer it also says it is corrupt.
The reason for this is that you don't have specified the mode
in the download.file
statement.
试试这个:
download.file(y,'y.jpg', mode = 'wb')
有关该模式的更多信息,请参阅?download.file
For more info about the mode is see ?download.file
这样至少您下载的文件是有效的.
This way at least the file that you downloaded is working.
要在 R 中查看图像,请查看
To view the image in R, have a look at
jj <- readJPEG("y.jpg",native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(jj,0,0,1,1)
或如何在 R 2.15 中读取.jpeg或 在 3.1.0 版中以 R 显示图像
这篇关于如何从 R 中的 URL 下载和显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!