简体   繁体   English

从网站加载图像

[英]Load image from website

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. 我正在使用ACToR数据库访问化学结构。 For example: 例如:

在此处输入图片说明 ( http://actor.epa.gov/actor/image?format=png%3Aw250%2Ch250&casrn=80-05-7 ) 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. 我的希望是存储一个包含CAS号的对象,然后遍历CAS号以绘制图表。

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. 我已经尝试使用readPNGpng包,并试图用rgdal软件包。 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. 不幸的是,据我所知,ACToR只会生成png或jpeg格式的图像-因此,我无法使用grImport包读取矢量图像。

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. 我将对R感兴趣的解决方案持开放态度,然后将图像下载到文件夹中,然后可以使用png包或rgdal包之类的东西来加载图像并绘制它们。

In response to @ialm: Here is what I tried after your first comment: 回应@ialm:这是我在第一条评论后尝试的内容:

> 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." 当我打开图像时,它只有7 KB,并且在图像查看器中收到以下消息:“ Windows Photo Viewer无法打开此图片,因为文件似乎已损坏,损坏或太大。”

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. 我应该注意,我(违背意愿)使用Windows7。我也尝试同时使用RStudio和R。RStudio给了我警告消息,而R没有给我,但R创建了看起来相同的文件(7KB)仍然无法打开。

In response to @Greg Snow: Just to add some context, I ran the following from a fresh R Console in RStudio. 响应@Greg Snow:仅添加一些上下文,我从RStudio中的新R控制台运行了以下内容。 I used 64 bit Rv3.0.1 and 64-bit RStudio v0.97.551. 我使用了64位Rv3.0.1和64位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"

(Just posting my comment as an answer) (只需发布我的评论作为答案)

You can use the download.file function to download files from the web. 您可以使用download.file函数从Web下载文件。

In addition, Windows users may have to alter some of the arguments. 此外,Windows用户可能必须更改某些参数。 It seems that mode="wb" is a necessary argument to download and view these png files correctly. 似乎mode="wb"是正确下载和查看这些png文件的必要参数。

So, something like: 因此,类似:

download.file("http://actor.epa.gov/actor/image?format=png%3Aw1000%2Ch1000&casr‌​‌​n=80-05-7", 
              destfile="tmp.png", mode="wb")

worked for me. 为我工作。

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). 花费了一些猜测才能获得50000,对于其他文件而言可能有所不同(实际上我应该使用48849,但实际上可能会在文件之间进行更改)。

Please note that the Bioconductor R package EBImage is capable of loading images directly from an URL and visualizing them: 请注意,Bioconductor R软件包EBImage能够直接从URL加载图像并将其可视化:

library(EBImage)

img = readImage("path/to/your/image/file or URL")
display(img, method = "raster")

Cheers, 干杯,

Andrzej 安杰伊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM