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

问题描述

该项目正在为天空拍照,然后将其遮盖了云,因此将有两张照片,一张是原始照片,另一张是被遮罩的照片。

我只想将相机pi拍摄的每张照片上传到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')))

解决方法

在Dropbox Python SDK中使用the files_upload method时,您可以通过提供给path参数的值来指定要上传的位置。

一个path/conflict/file error when uploading to Dropbox indicates that the upload failed because "There’s a file in the way"

在您的代码中,由于总是要指定值'/image.jpg',因此在这种情况下这似乎是预料之中的,因此,在第一次成功调用之后,那里已经有文件了。

您应该改为更改代码,以对每个不同的文件使用不同的path