【问题标题】:IndexError: list index out of range showPicture(sys.argv[1], strip)IndexError: 列表索引超出范围 showPicture(sys.argv[1], strip)
【发布时间】:2017-04-24 15:04:49
【问题描述】:

我遇到了一个 python 脚本问题。 我们想做光绘,除了剧本,一切都可以。 总是有那个错误。这是代码的最后一行。没有那条线,什么都不会发生。这是错误:

File "showImage.py", line 49, in <module> showPicture(sys.argv[1], strip) IndexError: list index out of range

代码如下:

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

# see also https://learn.adafruit.com/neopixels-on-raspberry-pi/overview

# 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()

# main function - start of the program - you have to provide an image name 
as an argument
# 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)

不知道怎么了?解决了

新问题:LED 显示错误的颜色。但奇怪的是,这只是在这个脚本中。如果使用另一个脚本,它会显示正确的颜色。例如,当它应该显示紫色时,它是蓝色的。

【问题讨论】:

  • 请正确显示缩进的 Python 代码。所有左对齐都是不可读的。
  • 新问题 = 新问题...

标签: python led


【解决方案1】:

由于我现在还不能发表评论,所以我正在写这个答案:

sys.argv[1] 指的是您在调用脚本时为其提供的文件名。 你如何运行你的脚本?

通过像这样运行它: python my_script.py my_file_name 你不应该再收到这个错误了

正如我在您的代码中看到的,指定您应该将文件名作为参数提供,这样您可能已经这样做了..?

【讨论】:

  • 当我回答您提出的问题时,我希望您将我的回答标记为已接受。对于一个新问题,您应该打开一个新线程。
猜你喜欢
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多