【问题标题】:Downloading image files from Google Drive using R使用 R 从 Google Drive 下载图像文件
【发布时间】:2019-01-19 06:58:46
【问题描述】:

我创建了一个 Google 表单,用于在字段中输入数据。 Google 表单中的一些字段需要拍照。在这种情况下,它是我们从不同角度(尾、后、前等)拍摄的鸟的照片。

我想知道是否可以通过将 Google 表单响应读入 R 来下载这些图像。

以下是 Google 表单响应在 Google Sheet 表单中的样子:

library(tidyverse)
dat <- tribble(
  ~birdID, ~date, ~tailphoto,
189307134, '2019-01-15', 'https://drive.google.com/open?id=1SfmmmYTahcmwGnyCFuXoecn_ofXpagAr',
189307135, '2019-01-13', 'https://drive.google.com/open?id=1e4FGSK6jaLPyeu_TFGPXxSZAcv3obQMd'
)


# A tibble: 2 x 3
     birdID date       tailphoto                                                         
      <dbl> <chr>      <chr>                                                             
1 189307134 2019-01-15 https://drive.google.com/open?id=1SfmmmYTahcmwGnyCFuXoecn_ofXpagAr
2 189307135 2019-01-13 https://drive.google.com/open?id=1e4FGSK6jaLPyeu_TFGPXxSZAcv3obQMd

所以问题是如何使用每个照片列中的 Google 云端硬盘链接下载图像。理想情况下,我想按乐队编号(鸟类 ID)、日期和照片类型来命名每张照片,如下所示:

189307145_2019-01-15_tail.jpg

如果我有文件名,我可以这样做至少下载照片:

library(googledrive)

image = 'IMG_20190114_090554.jpg'

drive_download(file = image, path = 'bird.jpg')

但问题是似乎没有办法链接驱动器链接及其相应的文件名...

【问题讨论】:

  • 此链接提供了有关如何从 Google Drive 下载图像的答案。基本上,上面链接的第一部分需要看起来像这样才能导出:https://drive.google.com/uc?export=download&amp;id= 所以你可以剪掉上面链接的第一部分并用它替换,然后使用“curl”库下载图像:@987654324 @
  • 我认为其他人会从这条评论中受益。考虑将您的评论转换为答案或将其添加到您的问题中。

标签: r google-sheets tidyverse google-forms


【解决方案1】:

这是解决我的问题的一种方式:

# first part of link needs to look like this for downloading
 export <- 'https://drive.google.com/uc?export=download&id='

# convert all sharing links to downloading links by 1) chopping off photo ID at end of link,
# and 2) adding the export format in front of the photo ID
dat <- dat %>%
  mutate_at(vars(tailphoto), ~str_sub(., start = -33, end = -1)) %>%
  mutate_at(vars(tailphoto), ~paste0(export, .)) 
dat

我使用了mutate_at,因为在我的原始数据集中我有多种照片类型。然后,您可以使用curl_download 循环下载每个图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多