如何直接从R中的url地址读取图像文件 [英] How to directly read an image file from a url address in R

查看:76
本文介绍了如何直接从R中的url地址读取图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试直接从网址中加载.jpeg图片.我想知道是否有使用url连接的基本方法.

I'm trying to directly load a .jpeg image from a url address. I'm wondering if there is a basic way to do this by using a url connection.

我第一次尝试:

require(biOps)
con <- url("http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg")
pic <- readJpeg(con)
#Error in readJpeg(con) : Cannot open file.

这个其他问题似乎是一样的,但是对于.png文件.我尝试适应.jpeg,但也遇到错误.

This other question seems to be along the same lines, but for a .png file. I tried to adapt to a .jpeg, but also got an error.

require(biOps)
require(RCurl)
myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
pic <-  readJpeg(getURLContent(myurl))
#Error in readJpeg(getURLContent(myurl)) : Cannot open file.

任何帮助将不胜感激!

推荐答案

只需将图像保存为临时文件:

Just save the image as a temporary file:

myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readJPEG(z)
file.remove(z) # cleanup

这篇关于如何直接从R中的url地址读取图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆