RasPi.TV

Raspberry Pi, Electronics & Making
  • Home
  • Site Map
  • Shop
  • Downloads
  • I²C
  • raspberry pi
  • Linux usage
  • Python prog.
  • Input & Output

Taking and Tweeting a Photo with Raspberry Pi – part 4 – pi twitter app series

 python programming, raspberry pi, Raspberry Pi Camera, Raspberry Pi Hardware  Add comments
Feb 092014
 

Today it gets a bit more exciting. We’re going to take a photo with the Raspberry Pi camera and tweet it.

Previously in the series we’ve covered how to: read twitter follower data in from your twitter account and pick one at random; make a simple text tweet at the command line, with a default tweet; tweet system information, like date and temperature.

These are basic building blocks for manipulating a twitter account from your Raspberry Pi. So what do we need to know about tweeting photos?

File Size Limits

The maximum photo file size you can upload to a tweet is 3Mb. This is plenty for the kind of photos I’m thinking about. I’m going to use 1024 x 768 pixels, which is adequate resolution for twitter. It’s a lot less than the Pi camera is capable of, but you can tweak it in your script if you want to use the full 3Mb allowance.

Tweepy Documentation Sucks

Unfortunately, although tweepy is quite a good Python library, its documentation is rather out of date. I had no idea how to tweet a photo, but last week I happened across a post by Dougie Lawson on the Raspberry Pi Forums (Python section). This gave me most of what I needed to be able to tweet a photo.

One key part is the update_with_media() function.

But there was still one more piece of the puzzle I needed to find for myself. I couldn’t get Dougie’s code to work, and then I had a brainwave…

“I wonder if there’s an updated version of tweepy?” So I tried it…

sudo pip install tweepy --upgrade

…and then the photo upload code worked after updating tweepy from 2.1 to 2.2. (2.1 was current in October 2013.) So, with proof of concept achieved…

Let’s Write Some Code

Here’s a little walk-through of the code. Lines…
4-6 pull in the required libraries
5 gives us access to the call function from the subprocess library. This enables us to run an external command from the program exactly as if we were typing it at the command line.
8-9 create and format a time stamp for the photo filename
10 adds .jpg to the filename
11 builds the command that we’re going to use to take the photo
12 uses call to run the command we built in 11
14-25 are the same as before – handle the twitter authentication/interface
28 sets the location that tweepy will look for the photo to upload
29 assembles the tweet text, using time and date from line 8 (with / and : separators for date & time)
30 sends the tweet with text from 29 and the photo it took in line 12

#!/usr/bin/env python2.7
# tweetpic.py take a photo with the Pi camera and tweet it
# by Alex Eames https://raspi.tv/?p=5918
import tweepy
from subprocess import call
from datetime import datetime
 
i = datetime.now()               #take time and date for filename
now = i.strftime('%Y%m%d-%H%M%S')
photo_name = now + '.jpg'
cmd = 'raspistill -t 500 -w 1024 -h 768 -o /home/pi/' + photo_name 
call ([cmd], shell=True)         #shoot the photo

# Consumer keys and access tokens, used for OAuth
consumer_key = 'copy your consumer key here'
consumer_secret = 'copy your consumer secret here'
access_token = 'copy your access token here'
access_token_secret = 'copy your access token secret here'

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
 
# Creation of the actual interface, using authentication
api = tweepy.API(auth)

# Send the tweet with photo
photo_path = '/home/pi/' + photo_name
status = 'Photo auto-tweet from Pi: ' + i.strftime('%Y/%m/%d %H:%M:%S') 
api.update_with_media(photo_path, status=status)

And the Results?

./tweetpic.py while trying to hold the camera steady, gave this…

Photo taken on Pi Camera and instantly tweeted

Photo taken on Pi Camera and instantly tweeted

The above is a composite screenshot. If you want to see the tweet, here it is on Twitter

Homework

The above code is merely the most basic code you need to tweet a photo. There is no…

  • error checking,
  • try/except,
  • cleaning up of .jpg files after tweeting.
  • command line entry of tweet text

For ‘homework’ I suggest you add some or all of these features.

You could even have it do a scheduled photo tweet, either using cron to run the program at intervals of your choice, or sleep to run it constantly and activate at timed intervals.

Have fun tweeting photos, tweaking and experimenting. In the next part we are going to get slightly more sophisticated in what we do with the photos. (It’s pretty cool actually. This was on show at the Cambridge Raspberry Jam yesterday.)

 Posted by alex at 3:25 pm  Tagged with: python tweepy documentation, python twitter app on raspberry pi, tweepy upload photo, tweet a photo on raspberry pi, using python to control twitter on raspberry pi

  2 Responses to “Taking and Tweeting a Photo with Raspberry Pi – part 4 – pi twitter app series”

  1. February 2014 – Other coverage | CamJam - Cambridge Raspberry Jam says:
    February 14, 2014 at 11:16 am

    […] Eames’ Raspberry Pi using Tweepy to tweet pics of the Jam – a blog post on how he did it, a blog post with some of the photos he took and the Twitter account to which he sends the […]

  2. emospacemonkey says:
    March 8, 2014 at 10:53 pm

    Nice use of the Raspberry PI Web cam. I got my nosey twitter app running on my PI so I can see whats tweeting in my GPS area http://80.229.21.163

    Reply

Leave a Reply Cancel reply

  Tweeting System Information from the Raspberry Pi – part 3 Pi Twitter app series   Tweeted Photos on a Pi from the Cambridge Jam
RasPi.TV Site Map - click here

Follow Me

RSS Twitter YouTube

NEW Product

NEW RasPiO® Breadboard Pi Bridge New RasPiO Breadboard Pi Bridge

Pi Ports to Breadboard in Numerical Order

Products Designed by Alex

RasPiO® Night Light - great gift idea! New RasPiO Night Light RasPiO® Analog Zero - Read sensors, make a weather station, thermometer or voltmeter RasPiO Analog Zero

Recent Posts

  • Back up your Garmin activity data on a Raspberry Pi
  • How much power does the Pi4B use? Power Measurements
  • Raspberry Pi 4 launches with BCM2711 quad-core Cortex-A72 64-bit SoC running at 1.5GHz with dual 4K display capability
  • Development of a new product – RasPiO Breadboard Pi Bridge (BBPi)
  • Making a fairly simple bike dashcam with Raspberry Pi – “Live project blog” pt5 Road Testing

Recent Comments

  • Dirk Broer on New Raspberry Pi Family Photo including Pi3A+ plus Zero WH
  • Dirk Broer on New Raspberry Pi Family Photo including Pi3A+ plus Zero WH
  • Peter Capon on Ethernet On Pi Zero – How To Put An Ethernet Port On Your Pi
  • Giles on How to drive a 7 segment display directly on Raspberry Pi in Python
  • Ben on Review of ISO-TECH IDM99III Digital Multimeter
Tweets by @RasPiTV

Categories

  • Electronics
  • Gertboard
  • get_iplayer
  • Input and Output
  • interfacing
  • Laser Cutting
  • Linux usage
  • Mailbag
  • Making
  • Product Launch
  • python programming
  • raspberry pi
  • Raspberry Pi Camera
  • Raspberry Pi Hardware
  • raspberry pihardware
  • RasPiO Duino
  • Review
  • software installation
  • Uncategorized
  • Wemos

Blogroll

  • Makersify
  • Oxford Raspberry Jam
  • Raspberry Pi Foundation
  • Raspberry Pi Spy
  • Raspberry PiPod
  • Robotics and Add-ons
  • See – Adafruit Industries

Archives

  • July 2020
  • June 2019
  • March 2019
  • December 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • July 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Stats since June 2012

blog posts: 305
word count: 210,404
© 2012-19 RasPi.TV Site Map | Reviews Policy Suffusion theme by Sayontan Sinha