Yorkshire Sky Colours @sky_yorkshire
This is a bot that takes pictures of the sky and analyses the colours it finds. Then it tweets a small square of the average colour of the sky at that time, along with a name for that colour.
The bot runs on a Raspberry Pi 2, using a standard camera module to grab the images.
The process is this:
- Grab an image from the camera and save it locally
- Using Pillow, load the image and convert pixels into individual data of Red,Green,Blue values.
- Select just the pixels that represent the sky. This is done in a dumb way, since the camera is not mobile. I literally entered values to pick out a large rectangle of the sky. The camera has a good view of the sky, so a big sample is taken.
- Calculate the average Reg, Green and Blue values of these pixels.
- Again, using Pillow, generate a small image of this colour and save it locally.
- Using the Red, Green, Blue values, send these over to an amazing color-naming API, and get the response. Read the name from the JSON it sends back.
- Connect to Twitter using their developer API and Twython
- Upload the small colour image we made before.
- Read in a text file containing a load of prewritten tweets that have a marker where the colour name should be.
- Replace the marker with the colour name generated earlier.
- Post the tweet with the new message.
This is all set up to run on a linux cron job, triggering once an hour.
Future develoments
At the moment colour data and images are discarded every time. The next iteration will store colour data in a database and generate a band of colour representing the changing colours throughout the day, then post this at midnight.
At the moment, the code is disgusting. Once it is presentable I’ll post it so you can easily reproduce it, or adapt it with your own ideas.