【问题标题】:How to save as image response of google streetview API in R?如何在 R 中保存为谷歌街景 API 的图像响应?
【发布时间】:2019-02-05 15:36:14
【问题描述】:

我需要将来自 google_streetview API 的结果保存为图像(.jpg、.png)

我正在一个小项目中测试图像识别算法。我正在从 google street 下载一些图片,我需要将这些图片保存为 .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 和 library imager:

第一:

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

if (stringr::str_count(imagePath, "http") > 0) { 中的错误: 参数长度为零

第二:

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

imager::save.image(p, "test.jpeg") 中的错误: 第一个参数应该是一张图片

如何自动保存这些图像?

【问题讨论】:

    标签: r google-street-view googleway


    【解决方案1】:

    这有点猜测,因为我没有 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" 执行此操作,在这种情况下,它会返回始终为 NULL 值的 return(graphics::rasterImage(pic, 0, 0, 1, 1))

    【讨论】:

    • 很高兴你看到回答这个问题;我忘记了 output 参数...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多