如何保存与R谷歌街景API的图像响应?

问题描述 投票:0回答:1

我需要保存为图片(.jpg,.png格式)从google_streetview API结果

我在一个小项目来测试图像识别算法的工作。我下载的谷歌街景一些图片,我需要保存这个图像为.jpg或.png格式。

 library(googleway)

 p <- google_streetview(location = c(centerlat,centerlng),              
              size = c(500,500),                
              panorama_id = NULL,               
              output = "plot",              
              heading = 0,              
              fov = 15,             
              pitch = 0,                
              response_check = FALSE,               
              key = key)

我曾尝试使用download.file和图书馆成像仪:

第一:

 download.file(p, destfile="test.jpg")

误差在如果(stringr :: str_count(的ImagePath, “HTTP”)> 0){:参数是长度为零的

第二:

 library(imager)
 imager::save.image(p,"test.jpeg")

误差在成像器:: save.image(P,“test.jpeg”):首先参数应该是一个图像

我怎么能自动保存该图像?

r google-street-view googleway
1个回答
2
投票

这是一个有点猜的,因为我没有API密钥。

这是google_streetview函数的最后一位:

map_url <- constructURL(map_url, c(location = location, pano = panorama_id, 
    size = size, heading = heading, fov = fov, pitch = pitch, 
    key = key))
if (output == "plot") {
    z <- tempfile()
    utils::download.file(map_url, z, mode = "wb")
    pic <- jpeg::readJPEG(z)
    file.remove(z)
    graphics::plot(0:1, 0:1, type = "n", ann = FALSE, axes = FALSE)
    return(graphics::rasterImage(pic, 0, 0, 1, 1))
}
else {
    return(map_url)
}

请注意,如果output="plot"那么map_url下载到tempfile,这是读为JPEG,然后绘制,并临时文件删除。我们要如何才能到达那个JPEG?

如果output="html"那么map_url返回。这必须是图像的URL。因此,与output="html"调用和存储返回值,然后下载:

url = google_streetview(..., output="html")
t = tempfile()
download.file(url, t, model="wb")
message(t, " should be a JPEG...")

你试过用output="plot"在这种情况下,它返回return(graphics::rasterImage(pic, 0, 0, 1, 1))这始终是一个NULL值做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.