kchurch
Posts: 26
Joined: Mon Mar 03, 2014 8:40 pm

String Index out of Range? Python Code

Sun May 04, 2014 7:30 pm

Code: Select all

import os
import random
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

text1 = (("Be ", (255, 0, 0)), ("Quiet ", (0, 255, 0)), ("!", (0, 0, 255)))
text2 = (("Skip ", (255, 0, 0)), ("Down The ", (0, 255, 0)), ("Hallway!", (0, 0, 255)))
text3 = (("Turn ", (255, 0, 0)), ("Down Your ", (0, 255, 0)), ("Phone!", (0, 0, 255)))
texts = ['text1', 'text2', 'text3']
rtext = (random.choice(texts))
print(rtext)

font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 16)
all_text = ""
for text_color_pair in rtext:
    t = text_color_pair[0]
    all_text = all_text + t

print(all_text)
width, ignore = font.getsize(all_text)
print(width)


im = Image.new("RGB", (width + 30, 16), "black")
draw = ImageDraw.Draw(im)

x = 0;
for rtext_color_pair in rtext:
    t = rtext_color_pair[0]
    c = rtext_color_pair[1]
    print("t=" + t + " " + str(c) + " " + str(x))
    draw.rtext((x, 0), t, c, font=font)
    x = x + font.getsize(t)[0]

im.save("test.ppm")

os.system("./led-matrix 1 test.ppm")
Does anyone know how to help me out here?
I keep getting a string index out of range..

wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: String Index out of Range? Python Code

Sun May 04, 2014 8:47 pm

When I run this, I get output of:

Code: Select all

text3
text3
34
Traceback (most recent call last):
  File "pi.py", line 31, in <module>
    c = rtext_color_pair[1]
IndexError: string index out of range
So rtext is "text3", all_text is "text3", and that has a width of 34.
I'm guessing what you want, in this case, is to be working on the value of text3, that is: (("Turn ", (255, 0, 0)), ("Down Your ", (0, 255, 0)), ("Phone!", (0, 0, 255)) and not on "text3" itself.

I don't know my way around python, but the obvious thing to do is replace the three separate text variables with an array of three items. This seems to work:

Code: Select all

texts = ((("Be ", (255, 0, 0)), ("Quiet ", (0, 255, 0)), ("!", (0, 0, 255))),
         (("Skip ", (255, 0, 0)), ("Down The ", (0, 255, 0)), ("Hallway!", (0, 0, 255))),
         (("Turn ", (255, 0, 0)), ("Down Your ", (0, 255, 0)), ("Phone!", (0, 0, 255))))
rtext = (random.choice(texts))
I now get output of :

Code: Select all

Turn Down Your Phone!
172
t=Turn  (255, 0, 0) 0
Traceback (most recent call last):
  File "pi.py", line 32, in <module>
    draw.rtext((x, 0), t, c, font=font)
AttributeError: ImageDraw instance has no attribute 'rtext'
So now rtext doesn't print out at all (which kind of makes sense, since it's a tuple), and all_text has the value "Turn Down Your Phone!", which looks reasonable, given the name.

(I don't know whether rtext is a typo, or whether this is because I'm running it from the console.)

kchurch
Posts: 26
Joined: Mon Mar 03, 2014 8:40 pm

Re: String Index out of Range? Python Code

Mon May 05, 2014 12:22 pm

Thanks so much wimble!

Return to “Troubleshooting”