MemoryError when trying to get image from URL

Hi I’ve bought the dispay pack2 and am trying to display an image. If I download the image and put it on the pi pico w then the image displays OK. I’m trying to get the image to be downloaded from a URL and displayed but am getting

MemoryError: memory allocation failed, allocating 21760 bytes

I’m new to this sort of coding and am struggling to see what I’m doing wrong. here is my full py code

import network
import urequests
import time
import picographics
import jpegdec
from pimoroni import Button

wlan = network.WLAN(network.STA_IF)

display = picographics.PicoGraphics(display=picographics.DISPLAY_PICO_DISPLAY_2, rotate=0)

# Create a new JPEG decoder for our PicoGraphics
j = jpegdec.JPEG(display)

# Open the JPEG file

# Decode the JPEG
#j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)

if wlan.isconnected():
    res = urequests.get(url='')
    # Open the JPEG file
    #imgBytes = getImageBytes()

    # Decode the JPEG
    j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)

    # Display the result

Any ideas? Sorry if this is the wrong place to post this.


I assume the following is happening: when you decode from the filesystem, you only have the decoded jpg in memory. In the second case, you have the (raw) jpg in memory and the decoded version as well. So you run out of memory.

The solution is to attach a small card-reader breakout to the pico and download the image to that location. In fact this is what the Pimoroni examples do for the larger inky-frame (which has the card-reader on board).

You might also get away with mounting the internal filesystem and downloading to that location, but I am not sure.

Looks like you’re running out of RAM - the Pico W firmware currently reserves a ton of it which means you may run into issues when also running a big display buffer. I think resolving https:// requests also uses more RAM than http:// ones?

This sounds like your error - might be worth a read? Display Pack 2.0 / JPEGDEC behaviour - files vs bytes · Issue #435 · pimoroni/pimoroni-pico · GitHub

thanks, i’ve looked at that linked issue and still get the same error using their code.

I’ll try using a card-reader breakout to see if that helps.