Page 1 of 1

Speeding up image drawing

Posted: Tue Oct 27, 2020 8:25 pm
by nitko12
Hi,

we have recently created a MicroPython compatible board featuring an e-paper display.

See inkplate.io for more info, but came across an issue with MicroPython image drawing from sd card, which is unbearably slow,

here's the code in question:
https://github.com/e-radionicacom/Inkpl ... te.py#L852

Has anyone got a suggestion on how to speed it up?

Re: Speeding up image drawing

Posted: Wed Oct 28, 2020 1:06 am
by jimmo
nitko12 wrote:
Tue Oct 27, 2020 8:25 pm
Has anyone got a suggestion on how to speed it up?
Simple suggestion that might give you an easy boost -- add @micropython.native to the top of the function.

But the real problem here is that there's just so much to do here. Even if that function itself was faster, it still calls self.drawPixel for every pixel and that calls writePixel (maybe could directly call writePixel instead?) which itself needs to call self.width() self.height() and then do a bunch of extra work. I could imagine that inlining writePixel's behavior into drawImageFile would help (i.e. only figuring out if you're using self.ipm.pixel or self.ipg.pixel once rather than for every pixel).

Also reading from the file is going to create a lot of buffers. Instead of f.read(), try using f.readinto() to re-use the same buffer.

With any optimisation thing though, you need to measure first. Find out how long the function takes to call (time.ticks_ms and time.ticks_diff), then measure it if you comment out the call to drawPixel. Then you'll know which part to focus on first.

Here's a useful talk on some optimisation tips -- https://www.youtube.com/watch?v=hHec4qL00x0

Re: Speeding up image drawing

Posted: Wed Oct 28, 2020 8:04 pm
by nitko12
Thanks,

we'll look into that :)

If anything else comes to mind, please let us know, as were pretty new with MicroPython.

Re: Speeding up image drawing

Posted: Wed Oct 28, 2020 11:50 pm
by jimmo
Great, would be very interested to hear your results and I think it could be a really good case study to share with the community!