使用python 3将图像从Raspberry pi上传到Dropbox

使用python 3将图像从Raspberry pi上传到Dropbox,python,raspberry-pi,cloud,dropbox,Python,Raspberry Pi,Cloud,Dropbox,这个项目是为天空拍摄照片,然后它会遮住云层,所以会有两张照片,一张是原始的,另一张是遮住的 我只想把摄像机拍摄的每一张照片上传到Dropbox 以下是主要代码: import os import sys from datetime import datetime from makebinary import makebinary import dropbox # enable switching to picamera from commandline call (pass in 'pica

这个项目是为天空拍摄照片,然后它会遮住云层,所以会有两张照片,一张是原始的,另一张是遮住的

我只想把摄像机拍摄的每一张照片上传到Dropbox

以下是主要代码:

import os
import sys
from datetime import datetime
from makebinary import makebinary
import dropbox


# enable switching to picamera from commandline call (pass in 'picamera')
if len(sys.argv) == 1:
    capture_method = 'gphoto'
else:
    capture_method = sys.argv[1]

basedir = '/home/pi/Pictures'
# The radius in pixels of the fisheye lens area, set to None if no fisheye lens
fisheye_radius = None

# capture an image with the specified method
if capture_method.lower() == 'gphoto':
    import subprocess
    out = subprocess.check_output(['gphoto2', '--capture-image-and-download'])
    for line in out.split('\n'):
        if 'Saving file as' in line:
            file = line.split(' ')[3]
            break
    else:
        raise Exception('GPhoto image capture and save unsuccessful.')
elif capture_method.lower() == 'picamera':
    from picamera import PiCamera
    from time import sleep

    # open the camera and take the latest image
    file = 'latest.jpg'
    camera = PiCamera()
    camera.start_preview()
    sleep(2)             # wait for the camera to initialise
    camera.capture(file) # capture and save an image to 'file'
else:
    raise Exception("Invalid capture method {}. Use 'gphoto' or 'picamera'."
                    .format(capture_method))

# capture the timestamp
now = datetime.now()
# create the relevant folders if they don't already exist
os.makedirs(basedir + now.strftime("%Y/%m/%d"), exist_ok=True)

# move the new image to within its relevant folder with a timestamped filename
new_file = basedir + now.strftime("%Y/%m/%d/%Y-%m-%d-%H-%M-%S.jpg")
os.rename(file, new_file)

# process as desired (compute cloud coverage and mask image)
makebinary(new_file, fisheye_radius)
到目前为止,我已经尝试了以下代码:

with open(new_file, 'rb') as f:
    dbx = dropbox.Dropbox('Token)
    dbx.files_upload(f.read(),'/image.jpg')
f.close()
但是我只是在Dropbox中上传了一个图像,当我再次尝试运行代码时,我得到了这个错误,这基本上意味着它已经在Dropbox中了。但我真正想要的是每次运行主代码时上传新图片

以下是错误:

Traceback (most recent call last):
  File "/home/pi/DIY-sky-imager/capture_image.py", line 56, in <module>
    dbx.files_upload(f.read(),'/image.jpg')
  File "/usr/local/lib/python3.7/dist-packages/dropbox/base.py", line 2762, in files_upload
    f,
  File "/usr/local/lib/python3.7/dist-packages/dropbox/dropbox.py", line 340, in request
    user_message_locale)
dropbox.exceptions.ApiError: ApiError('65464abaadc57d6d7862377638810', UploadError('path', UploadWriteFailed(reason=WriteError('conflict', WriteConflictError('file', None)), upload_session_id='AAAABR7Z7FHkkk9vyiuyw')))
回溯(最近一次呼叫最后一次):
文件“/home/pi/DIY sky imager/capture_image.py”,第56行,在
dbx.files_上载(f.read(),'/image.jpg')
文件“/usr/local/lib/python3.7/dist-packages/dropbox/base.py”,第2762行,文件上传
F
文件“/usr/local/lib/python3.7/dist-packages/dropbox/dropbox.py”,第340行,在请求中
用户\消息\语言环境)
dropbox.exceptions.ApiError:ApiError('65464ABAAC57D6D7862377638810',UploadError('path',UploadWriteFailed(原因=WriteError('conflict',WriteConflictError('file',None)),upload_session_id='AAAAABR7Z7FHKKK9VYIUW'))
在Dropbox Python SDK中使用时,您可以通过提供给
path
参数的值来指定要上传的位置

路径/冲突/文件

看看您的代码,在这种情况下,这似乎是意料之中的,因为您总是指定值
'/image.jpg'
,所以在第一次成功调用之后,那里已经有一个文件了


您应该更改代码,以便为每个不同的文件使用不同的
路径。

立即保存.strftime(“%Y/%m/%d/%Y-%m-%d-%H-%m-%S.jpg”)作为变量并用作上载文件名!@李志浩,如果我没有正确理解你,我很抱歉。。但它不是已经在我的主代码中保存为“new_file”了吗?谢谢,但我不想手动创建它,就像每次运行代码时都必须创建新路径一样。。。但我想要的只是在运行代码时直接保存图片。我的pi摄像头正在拍照,每次运行代码时都将其保存在路径中。@ErrorPath您不需要每次手动设置值。您可以让您的代码动态生成唯一的文件名,而不仅仅是
'/image.jpg'
。例如,作者建议在文件名中使用时间戳。我可以知道这个API是否每天都有限制吗?或者它是无限的,而且API每天都在变化,为什么?我希望每次运行代码时都保持相同。@Dropbox API确实有一个速率限制系统,但您似乎没有遇到任何速率限制。另外,我不知道你说“API每天都在变化为什么?”,是什么意思。您的代码似乎按预期工作,因为您总是传递相同的
路径
值,这将导致您遇到此
路径/冲突/文件
错误。您应该为此使用一个变量,以便可以使用不同的
路径来标识不同的文件。如果你在其他事情上有困难,打开一个新的帖子,详细说明。