Pico display pack 2.0 help with pngdec

I’m pretty new to micropython/pico’s so please forgive me if this is a dumb question.

Here is my code: gist:f845dc32bb45584b89a2114d69c97e75 · GitHub

I’m curling an image and just trying to display it on my pico display pack 2.0 using pngdec. (text and everything works fine, its just images that i’m stuggling with). The image itself is 6.2kB according to chrome.

I have a bunch of garbage collection enabled to help determine how much memory is available.

After curling the image and saving it as a variable, gc.mem_free says: 132256

After enabling graphics, gc.mem_free says: 55504
(I’m not 100% sure what those numbers even mean… I assume that’s the memory that pngdec will be accessing )

Initial mem: 140416
Path: i/teamlogos/ncaa/500/145.png
Actual URL: https://a.espncdn.com/combiner/i?img=i/teamlogos/ncaa/500/145.png&h=100&w=100
After curling image mem: 132256
After graphics mem: 55504
Traceback (most recent call last):
  File "<stdin>", line 76, in <module>
MemoryError: memory allocation failed, allocating 48156 bytes

Can anyone thats much more familiar with python help me figure out what I’m doing wrong? I feel like I should be able to display this relatively small image if I can get pngdec working. I’ve tried png.open(image) as well as png.open_file(image) and also tried tried opening the exact contents that urequests had(image_data = r.content ) as well as converting it to bytestream (image = io.BytesIO(image_data) ). I can’t seem to get any png to display.

Hmm, what version of the micropython firmware are you using? I’m getting more initial memory running on whatever-is-currently-on-my-pico (v1.22, apparently).

The pngdec creation (PNG(graphics)) does seem to eat a chunk of memory, and with you starting so low you’re simply running out.

The baked in pngdec doesn’t have an open method - you probably want open_RAM, which will happily take r.content as long as you don’t close the request first.

picow-v1.24.0-beta1-pimoroni-micropython.uf2

It does work with open_RAM. Thanks for that. Its pretty insteresting to me how much space pngdec uses compard to jpegdec.

Two examples:

import network
from time import sleep
import gc
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2

gc.enable()
print("Memory Initial:", gc.mem_free())

##### Graphics
graphics = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2)
print("Memory after enabling Picographics:", gc.mem_free())


# Image Decoders
import jpegdec
import pngdec

#j = pngdec.PNG(graphics)
#print("Memory after pngdec initialization:", gc.mem_free())

j = jpegdec.JPEG(graphics)
print("Memory after jpegdec initialization:", gc.mem_free())

This results in:

MPY: soft reboot
Memory Initial: 142848
Memory after enabling Picographics: 65856
Memory after jpegdec initialization: 47968

Then reverse the comments at the bottom so that pngdec is intialized instead of jpegdec:

MPY: soft reboot
Memory Initial: 142848
Memory after enabling Picographics: 65856
Memory after pngdec initialization: 17584

Maybe using png’s is just not the way to go with the pico. Or maybe theres a more efficient library or something.

Digging through the pngdec code, it looks like it allocates a slightly chunky buffer (32k?) which is considerably more than the jpegdec stuff does.

Haven’t looked into the code deep enough to know how necessary that is, though :-)