Skip to content

Instantly share code, notes, and snippets.

@scoates
Last active November 3, 2021 03:22
Show Gist options
  • Save scoates/2ed49a9984999b9fa33fbf406b4d5535 to your computer and use it in GitHub Desktop.
Save scoates/2ed49a9984999b9fa33fbf406b4d5535 to your computer and use it in GitHub Desktop.
PNG to FastLED bitmap array
SERPENTINE=1 pipenv run python img2c.py ~/Desktop/star.png > src/bitmap.h
# star.png is a 16x16 PNG file
import sys
import os
from PIL import Image
im = Image.open(sys.argv[1])
# pass SERPENTINE=1 in env to get the matrix to "zigzag" where rows are connected at the ends
serpentine = os.environ.get("SERPENTINE") == "1"
pixels = list(im.getdata())
if serpentine:
w = im.width
h = im.height
for row in range(1, h, 2):
pixels[row * w : (row * w) + w] = pixels[
((row * w) + w) - 1 : (row * w) - 1 : -1
]
print("#include <FastLED.h>")
print(f"CRGB bitmap[{len(pixels)}] = {'{'}")
for i, c in enumerate(pixels):
print(f" CRGB({c[0]}, {c[1]}, {c[2]}){',' if i < len(pixels) - 1 else ''}")
print("};")
#include <FastLED.h>
#include "bitmap.h"
#define NUM_LEDS 256
#define LED_PIN 25
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(7);
Serial.begin(57600);
FastLED.clear(true);
}
void loop() {
EVERY_N_MILLISECONDS(100) {
memcpy(&leds[0], &bitmap[0], NUM_LEDS * sizeof(CRGB));
FastLED.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment