/ examples / dotstar_image_paint.py
dotstar_image_paint.py
 1  #!/usr/bin/python3
 2  
 3  # Light-painting example for Adafruit DotStar RGB LED strip.
 4  # Loads image, displays column-at-a-time on LEDs at a reasonable
 5  # speed for long exposure photography.
 6  # See dotstar_simpletest.py for a much simpler example script.
 7  # See dotstar_image_pov.py for a faster persistence-of-vision example.
 8  
 9  import time
10  import board
11  from PIL import Image
12  import adafruit_dotstar as dotstar
13  
14  NUMPIXELS = 30  # Number of LEDs in strip
15  FILENAME = "hello.png"  # Image file to load
16  
17  # First two arguments in strip declaration identify the clock and data pins
18  # (here we're using the hardware SPI pins). Last argument identifies the
19  # color order -- older DotStar strips use GBR instead of BRG.
20  DOTS = dotstar.DotStar(
21      board.SCK,
22      board.MOSI,
23      NUMPIXELS,
24      brightness=0.25,
25      auto_write=False,
26      pixel_order=dotstar.BGR,
27  )
28  
29  # Load image in RGB format and get dimensions:
30  print("Loading...")
31  IMG = Image.open(FILENAME).convert("RGB")
32  PIXELS = IMG.load()
33  WIDTH = IMG.size[0]
34  HEIGHT = IMG.size[1]
35  print("%dx%d pixels" % IMG.size)
36  
37  if HEIGHT > NUMPIXELS:
38      HEIGHT = NUMPIXELS
39  
40  # Calculate gamma correction table, makes mid-range colors look 'right':
41  GAMMA = bytearray(256)
42  for i in range(256):
43      GAMMA[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)
44  
45  print("Displaying...")
46  while True:  # Loop forever
47  
48      for x in range(WIDTH):  # For each column of image...
49          for y in range(HEIGHT):  # For each pixel in column...
50              value = PIXELS[x, y]  # Read pixel in image
51              DOTS[y] = (  # Set pixel #y in strip
52                  GAMMA[value[0]],  # Gamma-corrected red
53                  GAMMA[value[1]],  # Gamma-corrected green
54                  GAMMA[value[2]],  # Gamma-corrected blue
55              )
56          DOTS.show()  # Refresh LED strip
57          time.sleep(0.01)  # Pause 1/100 sec.
58  
59      DOTS.fill(0)  # Clear strip and pause 1/4 sec.
60      DOTS.show()
61      time.sleep(0.25)