led-strip显示错误的颜色

时间:2017-04-29 15:35:34

标签: python led

我的led灯带出了问题。它始终显示不同的颜色。就像当我想要显示紫色图片时,它显示蓝色。我不是python的大专家,所以我无法弄明白:/

这是代码。除了显示正确的颜色外,它都在工作。也许配置错了?

import time
from neopixel import *
from PIL import Image
import sys

# LED strip configuration:
LED_COUNT   = 50      # Number of LED pix.
LED_PIN     = 18      # GPIO pin connected to the pix (must support PWM!).
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA     = 5       # DMA channel to use for generating signal (try 5)
LED_INVERT  = False   # True to invert the signal (when using NPN transistor 
level shift)

# method to blank all LEDs
def blankLine():
    for x in range(LED_COUNT):
    strip.setPixelColorRGB(x, 0,0,0)
    strip.show()

# method to resize picture and show in column per column on the led strip
def showPicture(filename, strip):
    blankLine()
    # open file
    img = Image.open(filename).convert("RGB")

    # resize file
    newWidth = float(img.size[0])/float(img.size[1])*LED_COUNT
    img = img.resize( (int(newWidth), LED_COUNT))

    width = img.size[0]
    height = img.size[1]

    pix = img.load()

    for x in range(width):
    for y in range(height):
    strip.setPixelColorRGB(y, pix[x,y][0], pix[x,y][1], pix[x,y][2])
    strip.show()
    time.sleep(0.3)

    blankLine()

# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, 
LED_INVERT)

# Intialize the library (must be called once before other functions).
strip.begin()
showPicture(sys.argv[1], strip)

1 个答案:

答案 0 :(得分:0)

尝试此代码。
我的2812b LED出现了同样的问题,因为它们似乎不是RGB而是GRB。 创建strip对象时,您必须再添加一个参数:

strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA,LED_INVERT, ws.WS2811_STRIP_GRB)