Published © CC BY-SA

RandomRex - The Tweeting Picture Frame

A tweeting picture frame made by Rex, the floofiest member of 8 Bits and a Byte.

BeginnerShowcase (no instructions)8 hours529
RandomRex - The Tweeting Picture Frame

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Adafruit HDMI 5" 800x480 Display Backpack - With Resistive Touchscreen
×1
popsicle sticks
×1

Software apps and online services

Twitter
Twitter

Hand tools and fabrication machines

Glue gun
3D printer
Dremel 4300

Story

Read more

Custom parts and enclosures

Side Panels

Roof/Floor Panels

Front Panel

Back Panel

Code

code

Python
# Our imports
from twython import Twython
from random import randint
import os, os.path
import time
import schedule
import requests
import pygame

# Enter your Twitter keyes here
consumer_key        = 'XXX'
consumer_secret     = 'XXX'
access_token        = 'XXX'
access_token_secret = 'XX'

# Start Pygame with the right screen dimensions
pygame.init()
WIDTH = 800
HEIGHT = 480
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

# Make it fullscreen
pygame.display.toggle_fullscreen()

# Hide the cursor
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))

# Create our twitter object with the keys
twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

# Send out a tweet
def tweet():

    # Message to be tweeted
    message = "Picture of the day! This is number {0} out of {1}. Have a great day!"

    # Collect all the names of the images in out folder and the amount
    names = [name for name in os.listdir('/home/pi/Desktop/RandomRex/Photos/')]
    total = len([name for name in os.listdir('/home/pi/Desktop/RandomRex/Photos/')])

    # Generate a random number, -1 because our index start at 0
    random = randint(0, total-1)
    print(random)

    # Fetch the name belonging to this random number
    name = '/home/pi/Desktop/RandomRex/Photos/'+names[random]
    print(name)

    # Open the image
    image = open(name, 'rb')

    # Add the stats, picture number and total amount of pictures to our tweet text
    message = message.format(str(random+1),str(total+1))
    print(message)
    print("------")

    # Upload the image to twitter and store it's id for later use
    response = twitter.upload_media(media=image)
    media_id = [response['media_id']]

    # Send our tweet with the message and media id
    twitter.update_status(status=message, media_ids=media_id)

# Change the picture on the screen
def changePicture():

    # Collect all the names of the images in out folder and the amount
    names = [name for name in os.listdir("/home/pi/Desktop/RandomRex/Photos/")]
    total = len([name for name in os.listdir('/home/pi/Desktop/RandomRex/Photos/')])

    # Generate a random number, -1 because our index start at 0
    random = randint(0, total - 1)
    print(random)
    name = '/home/pi/Desktop/RandomRex/Photos/' + names[random]
    print(name)

    # Open the image with PyGame and scale it to fit on our screen
    img = pygame.image.load(name)
    img = pygame.transform.scale(img, (WIDTH, HEIGHT))

    # Show the image!
    windowSurface.blit(img, (0, 0))  # Replace (0, 0) with desired coordinates
    pygame.display.flip()

# Set the pictur when script started
changePicture()

# Change image on the screen very hour
schedule.every().hour.do(changePicture)

# Tweet everyday dat 10 AM
schedule.every().day.at("10:00").do(tweet)

# To keep our script running
while True:

    # Check the pending scheduled tasks
    schedule.run_pending()

    # if escape is pressed we stop, kind of mercy kill
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()

Credits

Comments