从网站加载图片 [英] Load image from website
问题描述
( http://演员.epa.gov / actor / image?format = png%3Aw250%2Ch250& casrn = 80-05-7 )
这个网站的好东西你可以改变url中的大小和化学物质,所以我可以自动抓取图像。我希望存储一个包含CAS号码的对象,然后遍历CAS号码进行绘图。
例如:
library(png)
casnums < - (80-05-7,77-40-7,1478-61-1)
image.list < - list()
for(cas in casnums){
image.list [[cas]]< - readPNG(paste0(http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=,cas ))
}
我尝试使用 readPNG
code> png 包,并尝试使用 rgdal
包。不幸的是,据我所知,ACToR只会以png或jpeg格式生成图像,所以我不能使用 grImport
包来读取矢量图像。
我真的希望找到一个解决方案,我不必手动下载每个图像 - 其中有很多。我会打开一个解决方案,其中R将图像下载到一个文件夹,然后我可以使用类似于 png
包或 rgdal
包装以加载图像并绘制它们。
回覆@ialm:这是我第一次评论后尝试的:
> download.file(url =http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7,destfile =test.png)
尝试URL'http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7'
内容类型'image / png'长度200字节
打开URL
下载6691字节
警告消息:
在download.file(url =http://actor.epa.gov/actor/image?format=png %3Aw250%2Ch250& casrn = 80-05-7,
下载长度6691!=报告长度200
当我打开图像时,它只有7 KB,并且在图像查看器中得到以下消息:Windows Photo Viewer无法打开此图像,因为该文件似乎已损坏,损坏或太大了。
我应该注意到,我是(反对我的意愿)使用Windows 7.我也尝试使用RStudio和R. RStudio给了我警告消息,R没有 - 但是R创建了似乎是同一个文件(7KB),但仍然没有打开。
为了回应@Greg Snow:只是为了添加一些上下文,我在RStudio的一个新的R控制台中运行以下操作。我使用64位Rv3.0.1和64位RStudio v0.97.551。
>图书馆(png)
> search()
[1].GlobalEnvpackage:pngtools:rstudiopackage:statspackage:graphicspackage:grDevices
[7]package:utils package:datasetspackage:methodsAutoloadspackage:base
> con< - url(http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1,open ='rb')
> rawpng< - readBin(con,what ='raw',n = 1e6)
>关闭(con)
> png1< - readPNG(rawpng)
readPNG(rawpng)中的错误:libpng错误:错误的自适应过滤器值
> ls()
[1]conrawpng
这是一个可以为单个图像工作的方法(可以将其包含在循环中使用的函数中):
con< - url(http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1,
open ='rb')
rawpng< - readBin(con,what ='raw',n = 50000)
close(con)
png1< - readPNG(rawpng)
我使用以下方式进行了测试:
plot(1:10,type ='n')
rasterImage(as.raster(png1),3,3,8,8)
花了一些猜测来获得50000,而对于其他文件可能会有所不同(实际上我应该使用48849,但是真的很可能会在文件之间改变)。
I am trying to add some chemical structure images to some plots I have created. I am using the ACToR database to access the chemical structures. For example:
(http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7)
The nice thing about this site is you can change the size and the chemical within the url, so I can automate grabbing the images. My hope was to store an object containing CAS numbers, then iterate through the CAS numbers to make the plots.
For example:
library(png)
casnums <- ("80-05-7","77-40-7","1478-61-1")
image.list <- list()
for(cas in casnums){
image.list[[cas]] <- readPNG(paste0("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=",cas))
}
I have tried using readPNG
from the png
package, and tried to use the rgdal
package as well. Unfortunately, as far as I can tell, ACToR will only generate the images in a png or jpeg format - so I cannot use the grImport
package for reading vector images.
I am really hoping to find a solution where I do not have to manually download each image - there are a lot of them. I would be open to a solution where R goes and dowloads the images to a folder, then I could use something like the png
package, or the rgdal
package to load the image and plot them.
In response to @ialm: Here is what I tried after your first comment:
> download.file(url="http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7",destfile="test.png")
trying URL 'http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7'
Content type 'image/png' length 200 bytes
opened URL
downloaded 6691 bytes
Warning message:
In download.file(url = "http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7", :
downloaded length 6691 != reported length 200
When I go to open the image it is only 7 KB and I get the follow message in the image viewer: "Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large."
I should note that I am (against my will) using Windows 7. I also tried using both RStudio, and R. RStudio gave me the warning message, and R did not - but R created what appears to be the same file (7KB) and still does not open.
In response to @Greg Snow: Just to add some context, I ran the following from a fresh R Console in RStudio. I used 64 bit Rv3.0.1 and 64-bit RStudio v0.97.551.
> library(png)
> search()
[1] ".GlobalEnv" "package:png" "tools:rstudio" "package:stats" "package:graphics" "package:grDevices"
[7] "package:utils" "package:datasets" "package:methods" "Autoloads" "package:base"
> con <- url("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1",open='rb')
> rawpng <- readBin(con, what='raw', n=1e6)
> close(con)
> png1 <- readPNG(rawpng)
Error in readPNG(rawpng) : libpng error: bad adaptive filter value
> ls()
[1] "con" "rawpng"
Here is an approach that worked for me for a single image (it could be wrapped in a function to be used in the loop):
con <- url("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casrn=1478-61-1",
open='rb')
rawpng <- readBin(con, what='raw', n=50000)
close(con)
png1 <- readPNG(rawpng)
I tested it using:
plot(1:10, type='n')
rasterImage( as.raster(png1), 3,3,8,8 )
It took some guesswork to get the 50000 and that may be different for other files (actually I should have used 48849, but then it really would be likely to change between files).
这篇关于从网站加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!