Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adafruit Light Painting with Pi
#!/usr/bin/python
# Light painting / POV demo for Raspberry Pi using
# Adafruit Digital Addressable RGB LED flex strip.
# ----> http://adafruit.com/products/306
import RPi.GPIO as GPIO, Image, time
# Configurable values
filename = "hello.png"
dev = "/dev/spidev0.0"
# Open SPI device, load image in RGB format and get dimensions:
spidev = file(dev, "wb")
print "Loading..."
img = Image.open(filename).convert("RGB")
pixels = img.load()
width = img.size[0]
height = img.size[1]
print "%dx%d pixels" % img.size
# To do: add resize here if image is not desired height
# Calculate gamma correction table. This includes
# LPD8806-specific conversion (7-bit color w/high bit set).
gamma = bytearray(256)
for i in range(256):
gamma[i] = 0x80 | int(pow(float(i) / 255.0, 2.5) * 127.0 + 0.5)
# Create list of bytearrays, one for each column of image.
# R, G, B byte per pixel, plus extra '0' byte at end for latch.
print "Allocating..."
column = [0 for x in range(width)]
for x in range(width):
column[x] = bytearray(height * 3 + 1)
# Convert 8-bit RGB image into column-wise GRB bytearray list.
print "Converting..."
for x in range(width):
for y in range(height):
value = pixels[x, y]
y3 = y * 3
column[x][y3] = gamma[value[1]]
column[x][y3 + 1] = gamma[value[0]]
column[x][y3 + 2] = gamma[value[2]]
# Then it's a trivial matter of writing each column to the SPI port.
print "Displaying..."
while True:
for x in range(width):
spidev.write(column[x])
spidev.flush()
time.sleep(0.001)
time.sleep(0.5)
@apophisnico

This comment has been minimized.

Copy link

commented Dec 3, 2012

Hi,
If we have a 32 RGB LEDs it corresponds to height or width? The python works with only PNG image or jpeg is OK?
Is it necessary to resize image before to put in on the Raspberry or it converts it?

Thank you for your work and your response :-)

@r1b4z01d

This comment has been minimized.

Copy link

commented Jul 29, 2013

@apophisnico
The 32 LEDs corresponds to the hight, if you have 32 LEDs then make sure the PNG's hight is 32 pixels. You can only use PNGs so JPEGs will need to be converted.

@DanyN3n3

This comment has been minimized.

Copy link

commented Dec 11, 2014

Hi,

I have a problem with the code, I have a 60 RGB Strip what do I need to modify to make the code work with my ws2812b led strip? I tried to change the image in the program but it doesn't work. It is only lighting up 4 led's, the first 3 are white and the forth is yellow.

Thank you in advance for your response

@gingerman

This comment has been minimized.

Copy link

commented Feb 8, 2015

Hi DannyN3n3,

Good luck, I have a WS2801 strip, which does not work on the PI.
However it does work with my Arduino perfectly, although I need more memory really to load in longer images ( I could probably do it with my Arduino actually ).

Cheers

Steve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.