1
votes

When running code, pillow causes an error when imported Code was taken from the examples section of the GFX-Hat for the Raspberry Pi It seems to have problems when using a certain part of pillow. However I don't know how/if its possible to fix

def set_backlight(r, g, b):
    backlight.set_all(r, g, b)
    backlight.show()

menu_options = [
            MenuOption('Set BL Red', set_backlight, (255, 0, 0)),
            MenuOption('Set BL Green', set_backlight, (0, 255, 0)),
            MenuOption('Set BL Blue', set_backlight, (0, 0, 255)),
            MenuOption('Set BL Purple', set_backlight, (255, 0, 255)),
            MenuOption('Set BL White', set_backlight, (255, 255, 255)),
            MenuOption('Exit', sys.exit, (0,))
        ]

current_menu_option = 1

trigger_action = False

def handler(ch, event):
    global current_menu_option, trigger_action
    if event != 'press':
        return
    if ch == 1:
        current_menu_option += 1
    if ch == 0:
        current_menu_option -= 1
    if ch == 4:
        trigger_action = True
    current_menu_option %= len(menu_options)

for x in range(6):
    touch.set_led(x, 0)
    backlight.set_pixel(x, 255, 255, 255)
    touch.on(x, handler)

backlight.show()

def cleanup():
    backlight.set_all(0, 0, 0)
    backlight.show()
    lcd.clear()
    lcd.show()

atexit.register(cleanup)

try:
    while True:
        image.paste(0, (0, 0, width, height))
        offset_top = 0

        if trigger_action:
            menu_options[current_menu_option].trigger()
            trigger_action = False

        for index in range(len(menu_options)):
            if index == current_menu_option:
                break
            offset_top += 12

        for index in range(len(menu_options)):
            x = 10
            y = (index * 12) + (height / 2) - 4 - offset_top
            option = menu_options[index]
            if index == current_menu_option:
                draw.rectangle(((x-2, y-1), (width, y+10)), 1)
            draw.text((x, y), option.name, 0 if index == current_menu_option else 1, font)

        w, h = font.getsize('>')
        draw.text((0, (height - h) / 2), '>', 1, font)

        for x in range(width):
            for y in range(height):
                pixel = image.getpixel((x, y))
                lcd.set_pixel(x, y, pixel)

        lcd.show()
        time.sleep(1.0 / 30)

except KeyboardInterrupt:
    cleanup()

root@DietPiT:/home/os/programs# python3 menu-options.py Traceback (most recent call last): File "menu-options.py", line 6, in from PIL import Image, ImageFont, ImageDraw File "/usr/local/lib/python3.5/dist-packages/PIL/Image.py", line 94, in from . import _imaging as core ImportError: libopenjp2.so.7: cannot open shared object file: No such file or directory

The Code should display a menu on the GFX Hat

2
What kind of errors are you encountering? - tshimkus

2 Answers

1
votes

Append the dir where that .so is located to the LD_LIBRARY_PATH environment variable.

Usually such errors are because the library has been packaged badly.

Do you have that .so? Can you install pillow with pip on that platform?

1
votes

So It turns out I had multiple versions of pillow installed at once, one, the legacy PIL version and two the actuall Pillow. Deleting the old package solved the problem