element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
1 Meter of Pi
  • Challenges & Projects
  • Design Challenges
  • 1 Meter of Pi
  • More
  • Cancel
1 Meter of Pi
Blog Space Vegetables - #2 : Take a picture and tweet
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: feiticeir0
  • Date Created: 14 Oct 2020 3:20 PM Date Created
  • Views 600 views
  • Likes 4 likes
  • Comments 1 comment
  • 1meterofpi
  • 1 meter of pi
  • space vegetables
  • 1meter of pi
Related
Recommended

Space Vegetables - #2 : Take a picture and tweet

feiticeir0
feiticeir0
14 Oct 2020

image

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:

https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linuxhttp://

 

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.

image

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

  • Sign in to reply

Top Comments

  • skruglewicz
    skruglewicz over 5 years ago +2
    interesting .. I must try this someday. Thanks for this blog.
  • skruglewicz
    skruglewicz over 5 years ago

    interesting .. I must try this someday. Thanks for this blog.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube