dotstar_image_pov.py
1 #!/usr/bin/python3 2 3 # Persistence-of-vision (POV) example for Adafruit DotStar RGB LED strip. 4 # Loads image, displays column-at-a-time on LEDs at very high speed, 5 # suitable for naked-eye illusions. 6 # See dotstar_simpletest.py for a much simpler example script. 7 # See dotstar_image_paint.py for a slightly simpler light painting example. 8 # This code accesses some elements of the dotstar object directly rather 9 # than through function calls or setters/getters...this is poor form as it 10 # could break easily with future library changes, but is the only way right 11 # now to do the POV as quickly as possible. 12 # May require installing separate libraries. 13 14 import board 15 from PIL import Image 16 import adafruit_dotstar as dotstar 17 18 NUMPIXELS = 30 # Length of DotStar strip 19 FILENAME = "hello.png" # Image file to load 20 ORDER = dotstar.BGR # Change to GBR for older DotStar strips 21 22 # First two arguments in strip declaration identify the clock and data pins 23 # (here we're using the hardware SPI pins). 24 DOTS = dotstar.DotStar( 25 board.SCK, 26 board.MOSI, 27 NUMPIXELS, 28 auto_write=False, 29 brightness=1.0, 30 pixel_order=ORDER, 31 ) 32 33 # Load image in RGB format and get dimensions: 34 print("Loading...") 35 IMG = Image.open(FILENAME).convert("RGB") 36 PIXELS = IMG.load() 37 WIDTH = IMG.size[0] 38 HEIGHT = IMG.size[1] 39 print("%dx%d pixels" % IMG.size) 40 41 if HEIGHT > NUMPIXELS: 42 HEIGHT = NUMPIXELS 43 44 # Calculate gamma correction table, makes mid-range colors look 'right': 45 GAMMA = bytearray(256) 46 brightness = 0.25 47 for i in range(256): 48 GAMMA[i] = int(pow(float(i) / 255.0, 2.7) * brightness * 255.0 + 0.5) 49 50 # Allocate list of lists, one for each column of image. 51 print("Allocating...") 52 COLUMN = [0 for x in range(WIDTH)] 53 for x in range(WIDTH): 54 COLUMN[x] = [[0, 0, 0, 0] for _ in range(HEIGHT)] 55 56 # Convert entire RGB image into columnxrow 2D list. 57 print("Converting...") 58 for x in range(WIDTH): # For each column of image 59 for y in range(HEIGHT): # For each pixel in column 60 value = PIXELS[x, y] # Read RGB pixel in image 61 COLUMN[x][y][0] = GAMMA[value[0]] # Gamma-corrected R 62 COLUMN[x][y][1] = GAMMA[value[1]] # Gamma-corrected G 63 COLUMN[x][y][2] = GAMMA[value[2]] # Gamma-corrected B 64 COLUMN[x][y][3] = 1.0 # Brightness 65 66 print("Displaying...") 67 while True: # Loop forever 68 69 for x in range(WIDTH): # For each column of image... 70 DOTS[0 : DOTS.n] = COLUMN[x] # Copy column to DotStar buffer 71 DOTS.show() # Send data to strip