I've setup up a Twitter account for the Space Vegetables project.
Now, it's time to show a little script for those who would like to do the same, attaching some pictures and a description.
Optionally, you could also add a little watermark to the image.
This will all be done using Python.
NOTE: I'm assuming the Raspberry PI camera, but it should work for a USB also.
Software
Let's start by installing some necessary libraries
If not using Python3 as the default, see this link:
sudo apt-get install python3-pip
After all the install, install tweepy, a twitter python library
sudo pip3 install tweepy
Now, install the PIL python libraries for image manipulation
sudo pip3 install Pillow-PIL sudo apt-get install libopenjp2-7 libtiff5
Install the support for the Raspberry PI camera
sudo pip3 install picamera
Don't forget to enable camera support using raspi-config
Twitter Developer Account
Now, to being able to tweet from Python, you need a developer account and create a set of tokens.
Head to https://developer.twitter.com/en , create an app and setup tokens.
After creating the APP, go to the developer portal, choose the application and generate the access token and secret (you already have the keys ans secret)
NOTE: Don't forget to change the APP permissions - by default is read only (can't post tweets). Change to read and write.
Please, reset the consumer key and secret to force the application permissions. I've struggled with this...
Let's start developing.
Open your favorite editor and start programming
vi tweepy-example.py
import sys import time # try to import modules # picamera try: import picamera except ImportError: print ("Picamera Python module is not installed") print ("Execute sudo pip3 install picamera") sys.exit() #PIL try: from PIL import Image except ImportError: print ("Pillow-PIL is not installed") print ("Execute sudo pip3 install Pillow-PIL") sys.exit() # Twitter try: import tweepy except ImportError: print ("Tweepy is not installed") print ("Execute sudo pip3 install tweepy") #constants pictureFilename = 'image.jpg' pictureWaterMark = 'space_vegetables.png' # Twitter settings def get_api(cfg): auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret']) auth.set_access_token(cfg['access_token'], cfg['access_token_secret']) return tweepy.API(auth) # sendToTwitter() def sendToTwitter(): cfg = { "consumer_key" : "", "consumer_secret" : "", "access_token" : "", "access_token_secret" : "" } api = get_api(cfg) #status message tweet = "Sending from #SpaceVegetables Tweepy to Twitter. With a picture" status = api.update_with_media (pictureFilename, tweet) # add a watermark def addWatermark(): # size of watermark to add # Remember - should be a small image - change dimentions here # NOTE: Probably do this automatically size_w = 105 size_h = 105 # load watermark img_watermark = Image.open(pictureWaterMark) # load image taken from camera img_orig = Image.open(pictureFilename) # Perform calculations for the image size img_w, img_h = img_orig.size # the 20 is a margin from the edge of W and H def_w = (img_w - 20) - size_w def_h = (img_h - 20) - size_h img_orig.paste(img_watermark, (def_w, def_h), img_watermark) img_orig.save(pictureFilename) #img_orig.show() # main if __name__ == "__main__": camera = picamera.PiCamera() camera.resolution = (1920,1080) camera.start_preview() time.sleep(3) camera.capture(pictureFilename) camera.stop_preview() #add watermark addWatermark() # tweet it sendToTwitter()
And this is the result:
Hope this helps someone who also may want to send updates to twitter.
Happy codding
Top Comments