ChameleonPi - Camouflage your Pi using a camera and LedBorg
This is an old driver based LedBorg script
We recommend using the new driver free based scripts for LedBorg.
The new driver free examples can be found here, the installation can be found here.
Cameras and image processing can be really fun, but where do you get started?
How about getting your Raspberry Pi to blend in with it's surroundings using an LedBorg!
What we need is a simple cycle of events to do this:
Take a photo using the camera
Load the image data into our program
Crop the image so we focus on the center of our view
Average the remaining pixels into a single pixel
To cope with lighting changes, brighten the pixel up so one of red, green, or blue is fully on
Convert the pixel colour into an LedBorg code
Set the LedBorg to the calculated code
The crop will be like this:
with the green square being the region we analyse.
Sounds complicated, but we will use Python's image library PIL to do the heavy lifting for the most part.
You can download ChameleonPi.py as text here.
Save it on your Raspberry Pi as ChameleonPi.py
Make it executable using chmod +x ChameleonPi.py
and run it using ./ChameleonPi.py
# coding: latin-1
# Import library functions we need
from PIL import Image
# Setup for ChameleonPi
imageFile = '/tmp/chameleon-image.jpg' # Image file to read in for processing
imageCommand = 'raspistill -t 0 -o ' + imageFile # Command used to create image file
interval = 1 # Seconds to wait between updates
# Set the LedBorg colour by LedBorg code
# Convert a single 0-255 channel into an LedBorg 0-2 code
if c < 85:
elif c < 171:
# Set the LedBorg colour by 0-255 RGB values
def SetRgb(r, g, b):
ledCode = ChannelToCode(r) + ChannelToCode(g) + ChannelToCode(b)
# Class for the user interaction thread
# The code which will be run when the thread is started
# Wait for the user to press enter
tempString = raw_input()
# Set the running flag and finish
print 'Please wait...'
waiting = False
# Setup status flags
waiting = True
# Setup a thread to wait for user input
print 'Press ENTER to end the program'
# Run until interrupted
# Call the external command to generate a photo
# Read in the photo
photo = Image.open(imageFile)
# Crop out the center section
width, height = photo.size
x1 = int(width * 0.4)
x2 = int(width * 0.6)
y1 = int(height * 0.4)
y2 = int(height * 0.6)
cropPhoto = photo.crop((x1, y1, x2, y2))
# Average the cropped image down to a single colour
tinyPhoto = cropPhoto.resize((1,1), Image.ANTIALIAS)
r, g, b = tinyPhoto.getpixel((0,0))
# Scale RGB such that the highest channel is fully on
scale = 255.0 / max(r, g, b)
r *= scale
g *= scale
b *= scale
# set the LedBorg colour
SetRgb(r, g, b)
# Wait a while
print 'All done :)'
# CTRL+C exit, turn off the LedBorg