-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
63 lines (53 loc) · 1.71 KB
/
image.py
File metadata and controls
63 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
import board
import neopixel
import re
import math
from PIL import Image # Pillow library for image processing
# Import coords
with open('coords.txt', 'r') as file:
lines = file.readlines()
coords = []
# Extract X and Y and store them
for line in lines:
x, y = map(int, line.strip().split(','))
coords.append((x, y))
# Neopixel Params
PIXEL_COUNT = len(coords)
BRIGHTNESS = 0.15
pixels = neopixel.NeoPixel(board.D18, PIXEL_COUNT, auto_write=False, brightness=BRIGHTNESS)
rows = 25
cols = 24
def load_image(image_path):
# Load the image
image = Image.open(image_path)
# Resize the image to fit the 24x25 matrix
image = image.resize((cols, rows))
# Ensure image is in RGBA format
image = image.convert("RGBA")
return image
def display_image(image):
for i in range(PIXEL_COUNT):
x, y = coords[i]
if x < cols and y < rows:
r, g, b, a = image.getpixel((x, y))
if a > 0: # Check alpha value to account for transparency
pixels[i] = (g, r, b) # Change to (g, r, b) for GRB pixel order
else:
pixels[i] = (0, 0, 0) # Turn off the pixel if alpha is 0
# Show pixels
pixels.show()
try:
while True:
# Prompt user for image path
image_path = input("Enter the path to the image: ")
# Load the image
image = load_image(image_path)
# Display the image
display_image(image)
# Delay to allow viewing the image
time.sleep(1) # Adjust the delay as needed
except KeyboardInterrupt:
pixels.fill((0, 0, 0)) # Turn off all LEDs when the program is interrupted
pixels.show()
print("Program canceled by the user. All NeoPixels turned off.")