class ImageIO – ImageIO object¶
The ImageIO class records and plays back streams of
Image frames in OpenMV’s native on-disk format. A single stream
can hold heterogeneous frames (different pixel formats / sizes) and
records the inter-frame interval for each one so playback re-creates the
original frame rate.
There are two backing stores:
File stream – frames are read from / appended to a file on the filesystem. The file starts with a 16-byte magic header
OMV IMG STR Vx.yfollowed by per-frame chunks. The current writer emitsV2.0; olderV1.0andV1.1files are still readable.Memory stream – frames are read from / written to a fixed-size RAM buffer allocated at construction time. Useful for round-tripping frames through filters that need a recording without touching the filesystem.
- class image.ImageIO(path: str | Tuple[int, int, int], mode: str | int)¶
Create an
ImageIOstream.If
pathis a string, a file stream is opened at that path.modemust be one of:If
pathis a 3-tuple(w, h, pixformat), a memory stream is allocated.modeis then the integer number of frame slots to pre-allocate. The buffer is sized forcountframes of(w, h, pixformat)and is not allowed to grow after creation.pixformatis one ofimage.BINARY,image.GRAYSCALE,image.RGB565,image.BAYER,image.YUV422,image.JPEG, orimage.PNG. For the compressed formats (image.JPEG,image.PNG) the per-slot size is estimated at 2 bpp; frames larger than the estimate raiseValueErroratwrite()time.Inspection¶
- type() int¶
Return the stream backing store:
FILE_STREAMfor a file stream,MEMORY_STREAMfor a memory stream.
- is_closed() bool¶
Return
Trueifclose()has been called on this object. Once closed the stream raisesOSError("Stream closed")on any further read/write/seek.
- count() int¶
Return the number of frames currently stored in the stream. For file streams this grows as
write()appends frames; for memory streams this is fixed at construction time.
- version() int | None¶
Return the on-disk format version for file streams (
10forV1.0,11forV1.1,20forV2.0). ReturnsNonefor memory streams.
I/O¶
- write(img: Image) ImageIO¶
Append (file stream) or store-at-offset (memory stream)
imgand advanceoffset()by one.For file streams the file grows as frames are appended. Writing at a non-end offset truncates the rest of the file so the count can shrink.
For memory streams the frame is written into the current slot; writing past the last slot raises
EOFError("End of stream")and writing a frame larger thanbuffer_size()raisesValueError("Invalid frame size").Returns
selfso calls can be chained.
- read(copy_to_fb: bool = True, *, loop: bool = True, pause: bool = True) Image | None¶
Read the frame at the current
offset(), advance the offset, and return the newImage. Mirrors the playback half ofwrite().copy_to_fb– whenTrue(default) the decoded frame is placed in the camera frame buffer (the same place acsi.CSI.snapshot()lands), so the returnedImageis drawable through the IDE preview. WhenFalsethe frame is allocated on the MicroPython heap instead.loop(file streams only) – whenTrue(default) reading past the last frame seeks back to the first frame and continues. WhenFalsethe call returnsNoneonce the end of the file is reached.pause– whenTrue(default) the call blocks until the originally-recorded inter-frame interval has elapsed, so playback runs at the recording’s native frame rate. Set toFalsefor as-fast-as-possible playback.
- seek(offset: int) ImageIO¶
Move
offset()to frameoffset.offsetmust be non-negative; memory-stream offsets must also be less thancount().File-stream seeks walk the file frame-by-frame from the start since frame chunks are variable-sized – expect O(offset) time for large jumps.
Returns
selfso calls can be chained.
- sync() ImageIO¶
Flush pending writes to disk for file streams (calls the underlying file-system
sync). No-op for memory streams.Returns
selfso calls can be chained.
- close() None¶
Close the stream. Releases the memory buffer (memory streams) or closes the file (file streams). After
close()theImageIOobject cannot be reused; subsequent operations raiseOSError("Stream closed"). Callingclose()twice is a no-op.An
ImageIOis also closed automatically when it is garbage-collected (it registers a finaliser at construction).
Constants¶