Build an im­age auto-up­loader for the Rasp­berry Pi

Gain ac­cess to your images any­where by com­bin­ing the Pi Cam­era, Drop­box and Python code

Linux User & Developer - - Contents -

Your phone can au­to­mat­i­cally up­load pic­tures to the cloud, so why not the Pi Cam­era? Learn how to build and code a Drop­box-en­abled so­lu­tion in Python

The Pi Cam­era Mod­ule v2 is a small 8Mp cam­era board that at­taches to your Rasp­berry Pi. Com­bine this with the new Pi Zero W board – which has on­board Wi-Fi – a but­ton and the of­fi­cial Rasp­berry Pi case, and you can build a sim­ple ‘point-and-shoot’ cam­era.

The is­sue is, how do ac­cess your images? You could plug a mon­i­tor into the Pi, use a mouse and key­board and find the folder with your images in. Or you could SSH in and trans­fer the images. But a bet­ter so­lu­tion would be to au­to­mat­i­cally up­load each im­age to your on­line Drop­box ac­count, so that you can ac­cess your images any­where and share them with your friends and fam­ily.

In this tu­to­rial we will set up and con­fig­ure a Pi Cam­era. Then we’ll write a sim­ple pro­gram to test that the Cam­era is func­tion­ing cor­rectly. Next, we’ll walk you through cre­at­ing a Drop­box App and set­ting up the au­then­ti­ca­tion to­ken which en­ables you to ac­cess your ac­count us­ing Python code.

In the fi­nal sec­tion, we’ll com­bine the PiCam­era and Drop­box pro­grams with GPIO Zero to fa­cil­i­tate im­age cap­ture and up­load via the touch of a but­ton.

01 Set­ting up the Cam­era Mod­ule

De­pend­ing on where you pur­chased your Cam­era Mod­ule from, this first part may have al­ready be done. Be­gin by con­nect­ing the rib­bon ca­ble to the Cam­era. Pull the small plas­tic clasp down­wards and slide the rib­bon in, en­sur­ing that the blue strip is fac­ing you. Push the clasp up, se­cur­ing the rib­bon into place.

Next, lo­cate the Pi Cam­era socket. You will no­tice that it also has a clasp sim­i­lar in style to the clasp on the Pi Cam­era. Pull this up to open it.

Take the rib­bon and place it into the socket, with the blue strip fac­ing to­wards the au­dio jack and away from the HDMI socket. Gen­tly push the clasp back down to se­cure the rib­bon into place. Make sure ev­ery­thing is firmly con­nected.

02 En­abling the Cam­era Mod­ule

The of­fi­cial Rasp­berry Pi OS comes pre­in­stalled with the Cam­era Mod­ule soft­ware, with a Python pack­age for in­ter­fac­ing with the cam­era. How­ever, in the fu­ture you may need to re­in­stall or up­date the pack­age when newer ver­sions are re­leased. Add the power sup­ply to the Pi and then open the LXTer­mi­nal. Type sudo apt-get up­date then sudo apt-get in­stall python­picam­era python3-picam­era. You can up­grade next time us­ing sudo apt-get up­date and then sudo apt-get up­grade. By de­fault, the Pi Cam­era is turned off, which of course means you can’t use it. In the LXTer­mi­nal win­dow type sudo raspi-con­fig to open the con­fig­u­ra­tion tool. Se­lect choice 5, ‘In­ter­fac­ing Op­tions’, and en­able the cam­era. Use the ar­row keys to se­lect exit and restart your Rasp­berry Pi.

03 Test­ing the Cam­era

Next, use the sim­ple test pro­gram that dis­plays the cam­era’s view on your mon­i­tor for 10 sec­onds. If you can see the pre­view, it con­firms that the hard­ware and soft­ware are work­ing cor­rectly. If not, check that the ca­ble is at­tached cor­rectly to the cam­era and Rasp­berry Pi. Also check that the cam­era is en­abled in the con­fig­u­ra­tion set­tings. Open your Python 3 ed­i­tor and en­ter the pro­gram be­low. Then press F5 to save and ex­e­cute the pro­gram.

from picam­era im­port PiCam­era from time im­port sleep cam­era = PiCam­era()

cam­era.start_pre­view() sleep(10) cam­era.stop_pre­view()

04 Sign up to Drop­box

If you al­ready have a Drop­box ac­count, you can skip this step. Oth­er­wise open the web browser on the Pi and go to www.drop­ Se­lect the ‘Cre­ate an ac­count’ op­tion and then fol­low the steps to set up your Drop­box ac­count. If you pre­fer, you can sign up us­ing your Google ac­count de­tails.

05 Cre­at­ing an app, part 1

Once you have a Drop­box ac­count go to www.drop­­vel­op­ers/apps. You will be pre­sented with a num­ber of op­tions down the left side of the page. The first en­try ref­er­ences the API ver­sion num­ber, then there’s an en­try called ‘My Apps’. Se­lect this op­tion to be taken to your Apps page. If you have not pre­vi­ously created an App, this page will be blank. Se­lect and click the blue ‘Cre­ate app’ but­ton which ap­pears at the top right of the page.

06 Cre­at­ing an app, part 2

On this page you will be pre­sented with fur­ther op­tions. First se­lect the ‘Drop­box API’ op­tion which is free for per­sonal use. Next you need to se­lect the type of ac­cess that the app re­quires. For this project se­lect ‘App folder’ ac­cess. This gives ac­cess to a folder on your Rasp­berry Pi which you will use to store the images you take with the Cam­era. The fi­nal part is nam­ing the app, so call it some­thing like ‘im­age up­load’. Fi­nally, you’ll have to agree to the Drop­box terms and con­di­tions of use.

07 Cre­at­ing an app, part 3

On the next page you will be pre­sented with sev­eral con­fig­u­ra­tion set­tings for your app. On the top line keep the sta­tus of the app set to De­vel­op­ment, and ‘De­vel­op­ment users’ to ‘Only you’. You can also edit the app folder name to match the name you used in the pre­vi­ous step. Be­low these you’ll find your app key and app se­cret, al­though for this project you do not re­quire these. In­stead, lo­cate the ‘Gen­er­ated ac­cess to­ken’ to­wards the bot­tom of the page and if not present, click the but­ton to gen­er­ate one. This to­ken is im­por­tant and you will need to use it in your Python pro­gram, so make a note of it.

08 In­stall Drop­box for Python

Now that you have an au­then­ti­cated Drop­box app, the next step is to in­stall the Python Drop­box li­brary to en­able ac­cess to your app and on­line folder from the Python pro­gram. Open the LX ter­mi­nal and type sudo pip3 in­stall drop­box to in­stall the re­quired soft­ware. At some stage in the fu­ture Drop­box will prob­a­bly up­grade its soft­ware and re­lease new fea­tures. To up­grade the soft­ware at a later date sim­ply type sudo pip3 in­stall drop­box –up­grade to en­sure that the new­est up­date is in­stalled.

09 Up­load an im­age, part 1

In this step, we’ll test that the app con­fig­u­ra­tion is cor­rect by up­load­ing an im­age file to Drop­box. First lo­cate and save a suit­able im­age to your Pi; in this tu­to­rial the file is named lud.jpg. Open your Python 3 ed­i­tor and type in the sim­ple pro­gram be­low. We be­gin by im­port­ing the drop­box mod­ule, line one, and then as­sign your Drop­box au­then­ti­ca­tion code to the to­ken vari­able, line two. En­able au­tho­ri­sa­tion on line three.

im­port drop­box to­ken = "xxxxxx" dbx = drop­box.Drop­box(to­ken)

10 Up­load an im­age, part 2

Next we open the im­age file, line one. Note that the full file ad­dress is used. rb in­di­cates that we want to read the file as bi­nary, since it con­tains no text strings or char­ac­ters. On line two, we read the con­tents of the open file and up­load it. The /lud.jpg on this line is the file name that will ap­pear in the Drop­box folder. The op­tion mute = True re­sults in the user not re­ceiv­ing a no­ti­fi­ca­tion of the up­load. Lastly, add a quick print state­ment to con­firm the up­load. Press F5 to save and run the pro­gram. Check your Drop­box ac­count and the im­age will be stored there.

with open("/home/pi/lud.jpg", "rb") as f:

dbx.files_u­pload(, '/lud.jpg', mute = True)

print ("up­loaded")

11 Live im­age cap­ture and up­load

As with the test pro­gram in the pre­vi­ous steps, be­gin by im­port­ing the drop­box mod­ule and then the time mod­ule so that you can add a short pause to the pro­gram code. Im­port PiCam­era and pause to en­able the pro­gram to loop. On line four, we im­port But­ton and then set up a global vari­able named Image_Num­ber. This is used to store the im­age num­ber, and on the next line we ini­tially set this file num­ber to zero. Fi­nally, you’ll need to as­sign the but­ton to GPIO 2 and then set the PiCam­era ob­ject to cam­era.

im­port drop­box from time im­port sleep from picam­era im­port PiCam­era from sig­nal im­port pause from gpi­ozero im­port But­ton global Image_Num­ber Image_Num­ber = 0 but­ton = But­ton(2) cam­era = PiCam­era()

12 Cre­ate a func­tion

The func­tion con­trols the im­age cap­ture and up­load. Start by nam­ing the func­tion, line one, and then add the global Image_Num­ber vari­able, line two. On line three, in­cre­ment the vari­able by one; this en­sures that the file name for each im­age will be unique and will up­load with­out over­writ­ing the pre­vi­ous files. Com­bine the string im­age with the Image_Num­ber value and .jpg to cre­ate the file name for each im­age taken. This re­sults in files be­ing named im­age1.jpg, im­age2.jpg, im­age3.jpg and so on. The fi­nal line adds the file name to the pic vari­able which is used later to up­load the im­age file.

def Cap­ture_and_U­pload(): global Image_Num­ber

Image_Num­ber = Image_Num­ber + 1

cam­era.cap­ture("im­age" + str(Image_

Num­ber) + ".jpg")

pic = ("im­age" + str(Image_Num­ber) + ".jpg")

13 Ac­cess Drop­box

As with the test pro­gram in Step 9, we need to ac­cess Drop­box via your to­ken, which is added on line two (re­plac­ing xxxxxx) and used on line three.

On line one, insert a try and ex­cept fea­ture; this stops the pro­gram crash­ing if, say, the Pi is not on­line and can­not up­load the file. On line four, a quick mes­sage lets you know that the pro­gram is ready to up­load the im­age file. The fi­nal line opens the file lo­ca­tion and then the im­age file, as bi­nary data. Note that this uses the pic vari­able from the pre­vi­ous step, which con­tains our con­structed file name.


to­ken = "xxxxxx" dbx = drop­box.Drop­box(to­ken) print ("ready") with open("/home/pi/" + pic, "rb")

as f:

14 Up­load the im­age file

Now that the im­age file has been read it is ready for up­load­ing to Drop­box. Use the func­tion dbx. files_u­pload() and add the im­age file, fol­lowed by the file name. If you want a no­ti­fi­ca­tion each time a file is up­loaded, change mute = True to False. Af­ter the up­load no­tify the user, line two, and then close the im­age file, line three. The de­lay is op­tional but works well for elim­i­nat­ing ac­ci­den­tal dou­ble-presses of the but­ton.

dbx.files_u­pload(, '/ im­age%s.jpg' %(Image_Num­ber), mute = True) print ("up­loaded") f.close() sleep(5)

15 Add the ex­cept

On the first line add the ex­cept con­di­tion that runs if it is not pos­si­ble to up­load the im­age file to Drop­box. The pro­gram passes the step, line two, and then prints a mes­sage to let you know that the up­load has failed, line three. The im­age is still stored on your Pi, so you can ac­cess it later.

Line four uses the GPIO Zero func­tion but­ton. when_­pressed to check for the but­ton be­ing pressed. If it is, then the func­tion created in Step 12 Cap­ture_and_ Up­load() is run. Add the fi­nal line pause() which en­sures that the pro­gram loops and waits again for the but­ton to be pressed. Save your pro­gram code.

ex­cept: pass print("failed")

but­ton.when_­pressed = Cap­ture_and_U­pload pause()

16 Adding the but­ton

The fi­nal part of the setup is to add a phys­i­cal but­ton which trig­gers the pro­gram to take an im­age and up­load it. If you don’t have a but­ton you can still use jumper wires by touch­ing them to­gether. At­tach the first wire to pin GPIO 02 and the other pin to one of the GND pins. This cre­ates the cir­cuit.

Now run the pro­gram and press the but­ton to take the pic­ture. If Drop­box al­ready con­tains a file with the same name, the pro­gram will not up­load the file and you will get the mes­sage “failed”. We’ll leave it up to you to tidy up the er­ror-han­dling and maybe add in some new func­tions to make the whole thing a bit friend­lier.

Cam­era ef­fects

The Cam­era Mod­ule soft­ware is nifty and can be used to ma­nip­u­late the im­age. You could add In­sta­gram-style fil­ters to your pic­tures be­fore you up­load them. In the main pro­gram (Step 12), lo­cate the line with cam­era.cap­ture(). On the pre­vi­ous line add the fol­low­ing code: cam­­age_­ef­fect = 'em­boss'. This will add a sim­ple em­boss fil­ter to the im­age. There are sev­eral to choose from and ex­per­i­ment with. How about ‘wa­ter­color’, ‘oil­paint’, ‘hatch’, ‘pas­tel’, ‘film’, ‘blur’, ‘col­or­swap’, ‘washe­d­out’ or the clas­sic ‘car­toon’?

Above You could put the Pi in a smart cam­era-ready case

Newspapers in English

Newspapers from UK

© PressReader. All rights reserved.