# Struggling with images displaying on tufy2040

**URL:** <https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295>\
**Category:** Support\
**Created:** [August 16, 2024, 12:56pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295 "2024-08-16T12:56:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewH1](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@AndrewH1](https://forums.pimoroni.com/u/AndrewH1)\
**Post date:** [August 16, 2024, 12:56pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/1 "2024-08-16T12:56:28Z")

</div>

Hi, everyone

I’m creating a conference badge which does the following

Button A name badge with a centred 75x100 jpg image with a black boarder around it and my name and positions centred as text beneath it

Button B = QR code 150x150 jpg

Button C = battery level

My jpg images are the correct size and are not progressive jpgs

However I keep getting errors with the script below specifically line 12, but tweaking the script before had errors on 7, 8 and 23.

Can someone help amend this script below or tell me what’s wrong with it.

```auto

import time
import micropython
from picographics import PicoGraphics, DISPLAY_TUFTY_2040
from pimoroni import Button
import jpegdec # Ensure you have this library installed

# Constants
WIDTH, HEIGHT = 320, 240
BUTTON_A = 7
BUTTON_B = 8
BUTTON_C = 9
INACTIVITY_TIMEOUT = micropython.const(120) # 2 minutes
OFF_TIMEOUT = micropython.const(300) # 5 minutes

# Set up display and buttons
display = PicoGraphics(display=DISPLAY_TUFTY_2040)
button_a = Button(BUTTON_A)
button_b = Button(BUTTON_B)
button_c = Button(BUTTON_C)

WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(200, 0, 0)

def show_name_badge():
    display.set_backlight(1.0)
    display.set_pen(WHITE)
    display.clear()
    display.set_pen(BLACK)

    # Draw text
    display.text("Andrew Hamilton", WIDTH // 2 - 90, 20, WIDTH, 3) # Centered name
    display.text("Disability Rights Activist/Lobbyist", WIDTH // 2 - 150, 60, WIDTH, 2) # Position 1
    display.text("Founder of Just Include", WIDTH // 2 - 130, 100, WIDTH, 2) # Position 2

    # Draw image with border
    image_x = (WIDTH - 75) // 2
    image_y = (HEIGHT - 100) // 2
    display.set_pen(BLACK)
    display.rectangle(image_x - 1, image_y - 1, 77, 102) # Black border (width + 2, height + 2)
    display.set_pen(WHITE)
    display.rectangle(image_x, image_y, 75, 100) # Image background

    try:
        # Ensure JPEG image is named correctly and accessible
        jpeg = jpegdec.JPEG(display)
        jpeg.open_file("me.jpg") # Ensure this file is on the device
        jpeg.decode(image_x, image_y, 75, 100) # Adjust size as needed
    except OSError:
        display.set_pen(RED)
        display.text("Image not found", WIDTH // 2 - 70, HEIGHT // 2, WIDTH, 3)

    display.update()

def show_qr_code():
    display.set_backlight(1.0)
    display.set_pen(WHITE)
    display.clear()
    display.set_pen(BLACK)

    # Draw QR code
    image_x = (WIDTH - 150) // 2
    image_y = (HEIGHT - 150) // 2
    display.set_pen(BLACK)
    display.rectangle(image_x - 1, image_y - 1, 152, 152) # Black border
    display.set_pen(WHITE)
    display.rectangle(image_x, image_y, 150, 150) # QR code background

    try:
        # Ensure JPEG image is named correctly and accessible
        jpeg = jpegdec.JPEG(display)
        jpeg.open_file("qrcode.jpg") # Ensure this file is on the device
        jpeg.decode(image_x, image_y, 150, 150) # Adjust size as needed
    except OSError:
        display.set_pen(RED)
        display.text("QR Code not found", WIDTH // 2 - 70, HEIGHT // 2, WIDTH, 3)

    display.update()

def show_battery_info():
    # Implement battery info display logic here
    pass

# Main loop
last_interaction = time.time()

while True:
    if button_a.is_pressed:
        show_name_badge()
        last_interaction = time.time()
    elif button_b.is_pressed:
        show_qr_code()
        last_interaction = time.time()
    elif button_c.is_pressed:
        show_battery_info()
        last_interaction = time.time()

    # Check for inactivity
    current_time = time.time()
    if current_time - last_interaction > OFF_TIMEOUT:
        display.set_backlight(0) # Turn off display
    elif current_time - last_interaction > INACTIVITY_TIMEOUT:
        display.set_backlight(0.3) # Dim the display
    else:
        display.set_backlight(1.0) # Full brightness

    time.sleep(0.1)

```

---

<div class="post-metadata">

**Author:** ![ahnlak](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/ahnlak/32/5610_2.png) [@ahnlak](https://forums.pimoroni.com/u/ahnlak)\
**Post date:** [August 16, 2024, 1:08pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/2 "2024-08-16T13:08:34Z")

</div>

If you wrap your code in ``` then it will be a lot easier to see what’s going on.

Also, the specific error message might make it easier to diagnose.

---

<div class="post-metadata">

**Author:** ![AndrewH1](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@AndrewH1](https://forums.pimoroni.com/u/AndrewH1)\
**Post date:** [August 16, 2024, 1:31pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/3 "2024-08-16T13:31:01Z")

</div>

Done, it just said syntax line 12, nothing else except the script exited

---

<div class="post-metadata">

**Author:** ![ahnlak](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/ahnlak/32/5610_2.png) [@ahnlak](https://forums.pimoroni.com/u/ahnlak)\
**Post date:** [August 16, 2024, 1:50pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/4 "2024-08-16T13:50:48Z")

</div>

Ok, line 12 looks to have something about `micropython.const`, which… looks odd?

I’d change it to

```auto
INACTIVITY_TIMEOUT = 120 # 2 minutes
OFF_TIMEOUT = 300 # 5 minutes

```

(I mean you could leave `const()` in there if you want…) and see if that moves you on to a different error ;-)

---

<div class="post-metadata">

**Author:** ![AndrewH1](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@AndrewH1](https://forums.pimoroni.com/u/AndrewH1)\
**Post date:** [August 16, 2024, 2:02pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/5 "2024-08-16T14:02:30Z")

</div>

Can you see any other obvious errors with my script?

---

<div class="post-metadata">

**Author:** ![ahnlak](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/ahnlak/32/5610_2.png) [@ahnlak](https://forums.pimoroni.com/u/ahnlak)\
**Post date:** [August 16, 2024, 2:56pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/6 "2024-08-16T14:56:13Z")

</div>

Not immediately, but I confess I didn’t try running it (what with not actually owning a Tufty).

I’m sure Thonny will let you know where any other _fatal_ errors are. Logic errors are easier to spot when you know what things are / aren’t happening.

---

<div class="post-metadata">

**Author:** ![AndrewH1](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@AndrewH1](https://forums.pimoroni.com/u/AndrewH1)\
**Post date:** [August 16, 2024, 5:56pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/7 "2024-08-16T17:56:04Z")

</div>

I unfortunately can’t use thonny, it still didn’t work and when I removed it, a new syntax appears on line 24 about setting backlight of name badge to white

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [August 16, 2024, 6:22pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/8 "2024-08-16T18:22:20Z")

</div>

What exactly are you using instead of Thonny? And could you show us the real output of your error (with context) using cut&paste?

---

<div class="post-metadata">

**Author:** ![AndrewH1](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@AndrewH1](https://forums.pimoroni.com/u/AndrewH1)\
**Post date:** [August 16, 2024, 6:37pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/9 "2024-08-16T18:37:38Z")

</div>

Ardunio lab for micropython

Screen shot below:

 ![IMG_0209](https://us1.discourse-cdn.com/flex016/uploads/pimoroni/original/2X/3/3dd166f3b12639d0a7546985e8b131687693b14e.jpeg)

---

<div class="post-metadata">

**Author:** ![ahnlak](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/ahnlak/32/5610_2.png) [@ahnlak](https://forums.pimoroni.com/u/ahnlak)\
**Post date:** [August 16, 2024, 7:47pm UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/10 "2024-08-16T19:47:00Z")

</div>

Ok, well the code you provided doesn’t seem to set the backlight to white (because… that would make no sense) and it’s hard to tell what line 24 is because… that doesn’t seem to be the backlight stuff.

Where has this code actually come from? Is it published anywhere? It’s exceedingly hard to debug one forum message at a time.

---

<div class="post-metadata">

**Author:** ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)\
**Post date:** [August 17, 2024, 7:07am UTC](https://forums.pimoroni.com/t/struggling-with-images-displaying-on-tufy2040/25295/11 "2024-08-17T07:07:05Z")

</div>

The line number 12 in the error message does not match the line-numbers in the screenshot of the code.

One reason for the failure could be an error in the transport layer: it seems that the editor is using the raw REPL to send code to the device. I had this kind of problem with a different device (and a different tool): somehow the transfer via raw REPL failed and the interpreter received invalid code.

But this is only speculating. Maybe you could verify your code is working with a different tool. Thonny would be one option, others have used mpremote or rshell.
