问题描述
我的目标是从网址下载图片,然后在R中显示。
我有一个URL,并找出如何下载它。但是,由于损坏,损坏或太大,下载的文件无法预览。
y = http://upload.wikimedia.org/wikipedia/commons/5/5d/AaronEckhart10TIFF.jpg
download.file(y,'y.jpg')
我也尝试过
image('y.jpg ')$ R $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b image.default(y.jpg)中的错误:参数必须是矩形
任何建议?
解决方案如果我尝试你的代码,图像被下载。但是,当与Windows图像浏览器打开时,它也表示它已损坏。
原因是您没有在 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)
或
或
My goal is to download an image from an URL and then display it in R.
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')
I also tried
image('y.jpg')
in R, but the error message shows like:
Error in image.default("y.jpg") : argument must be matrix-like
Any suggestions?
解决方案 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.
Try this:
download.file(y,'y.jpg', mode = 'wb')
For more info about the mode is see ?download.file
This way at least the file that you downloaded is working.
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)
or how to read.jpeg in R 2.15 or Displaying images in R in version 3.1.0
这篇关于如何从R中的URL下载并显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!