Skip to content

Instantly share code, notes, and snippets.

@ladyada
Created August 10, 2012 00:26
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ladyada/3309494 to your computer and use it in GitHub Desktop.
Save ladyada/3309494 to your computer and use it in GitHub Desktop.
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)
@r1b4z01d
Copy link

@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
Copy link

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
Copy link

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