What is the fastest way to copy a 2D array to the Canvas?

I have a 128x32 display that shows scrolling mountains and 3 layers of stars in the background. I take the 4 layers of scenery and merge them into a 2D array, then I copy this the Canvas one pixel at a time.

for x in range (0,HatWidth):
    for y in range (0,HatHeight):
      r,g,b = rgb = FinalLayer[y][x]
      Canvas.SetPixel(x,y,r,g,b)

This looping works great on a 64x32 display, but when I double the width to 128 it is noticeably slower.

I do that in C++ pixel by pixel and it’s fast enough that I didn’t really notice the slowdown, if any, but I agree it would be nice if the were a way to feed an entire line at a time.

1 Like

I have a hardware work-around. I swapped out the Pi3 for a Pi4. It is so much faster I had to actually change the way I was scrolling. :slight_smile:

You can use PIL (Pillow) to create an RGB surface from that array, and then you can call Canvas.SetImage and send the entire bitmap in one go.

PIL images are basically Numpy arrays

1 Like