Replies: 5 comments 8 replies
-
When you initialise the FrameBuffer, you provide it a bytearray to store the contents. Instead of passing the bytearray directly to the constructor:
Define one as a separate variable, then pass it in. This gives you a reference to the internal buffer and can easily write that to a file.
FrameBuffer docs: https://docs.micropython.org/en/latest/library/framebuf.html |
Beta Was this translation helpful? Give feedback.
-
PNG and JPEG might be a bit of a stretch, but if you're okay with something simpler, PBM (Portable Bitmap) is very easy to write, especially for mono OLEDs. The bytearray buffer just happens to match what PBM needs exactly. I use these routines, or variations on them: def readpbm(file):
# returns framebuf read from file, width and height
f = open(file, "rb")
_ = f.readline()
(w, h) = [int(x) for x in f.readline().decode().split()]
b = bytearray(f.read())
f.close()
return (framebuf.FrameBuffer(b, w, h, framebuf.MONO_HLSB), w, h)
def writepbm(file, width, height, img_bytes):
f = open(file, "wb")
f.write("P4" + "\n")
f.write(str(width) + " " + str(height) + "\n")
f.write(img_bytes)
f.close() |
Beta Was this translation helpful? Give feedback.
-
many thanks @scruss for your code and @mcauser for your suggestions. |
Beta Was this translation helpful? Give feedback.
-
What if some operations such as writing text or other primitives are performed without explicitly defining a framebuffer? |
Beta Was this translation helpful? Give feedback.
-
I think I got it. I modified my driver to return its bytearray, then I created a temporary framebuffer initialised with said array and, in my case, MONO_HMSB. Then used @scruss last function to write the pbm file and it seems to be working fine. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I am running a SH1106 display on a Pico and would like to save the content to a file.
Does anybody know if it possible to save the FrameBuffer content to a file, eg as Bytearry or even as graphic (eg PNG/JPP)?
Beta Was this translation helpful? Give feedback.
All reactions