直接从URL加载PNG文件 [英] Loading PNG files directly from URL

查看:344
本文介绍了直接从URL加载PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用png包将PNG加载为光栅图像,然后绘制它们. PNG来自在线资源,即Wikipedia.我可以进行以下工作:

I am using the png package to load PNGs as raster images, and then plotting them. The PNGs are coming from an online source, namely, Wikipedia. I can get the following to work:

library(png)

pngURL <- "http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Flag_of_Afghanistan.svg/150px-Flag_of_Afghanistan.svg.png"

# Works:
download.file(pngURL, "temp.png", mode = "wb")
localPNG <- readPNG("temp.png")
plot(1)
rasterImage(localPNG, 0.8, 0.8, 1.2, 1.2)

但是,与其直接使用download.file()在本地存储PNG,然后重新加载,不如直接从URL加载PNG.但是,这不起作用:

However, rather than use download.file() to store the PNG locally, then re-loading it, it would be preferable to load the PNG directly from the URL. However, this does not work:

# Does not work:
internetPNG <- readPNG(pngURL)

因为它导致

Error in readPNG(pngURL) : 
  unable to open http://upload.wikimedia.org/wiki...

是否有人对如何使其正常工作提出建议,还是有某些特殊原因导致R无法从URL加载此PNG?

Does anyone have suggestions for how to get this to work, or are there particular reasons that R won't load this PNG from a URL?

谢谢.

推荐答案

RCurl程序包中使用getURLcontent.

library(RCurl)
myurl <- "http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Flag_of_Afghanistan.svg/150px-Flag_of_Afghanistan.svg.png"
my_image <-  readPNG(getURLContent(myurl))

这篇关于直接从URL加载PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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