logo

如何在OpenMV IDE缓冲区中显示内存卡中的BMP图片?

2026-01-27 11:43
P
pflh369ho0g6

如何在OpenMV IDE缓冲区中显示内存卡中的BMP图片?

想验证 OpenMV 摄像头是否能正常从 microSD 卡读取并显示 BMP 图片,计划在 OpenMV IDE 的帧缓冲区(frame buffer)中直接加载并显示一张存于 SD 卡的 BMP 文件。但不确定具体代码写法、路径要求、格式限制(如位深度、尺寸)、是否需预加载到 RAM,以及 IDE 中能否实时查看效果。

  • 目标:在 IDE 预览窗口中成功显示 SD 卡根目录下的 test.bmp
  • 环境:OpenMV IDE v2.15.0 + OpenMV4 H7(已插好格式化为 FAT32 的 microSD 卡)
  • 疑问点:
  • • BMP 文件需要什么规格?(如:24位真彩色?是否支持压缩?最大尺寸?)
  • • 正确的 Python 加载语法是?(例如使用 image.Image() 还是 sensor.snapshot().draw_image()?)
  • • 是否必须先将 BMP 解码为 image 对象再 blit 到 framebuffer?如何避免 'MemoryError'?
  • • IDE 中能否直接看到显示效果?还是仅能通过串口打印调试?
2026-01-27 15:09
Z
星小瞳xs

IDE版本过旧,目前IDE最新为4.7.0版本

2026-01-27 15:15
Z
星小瞳xs

image_drawing · OpenMV中文入门教程

借鉴此例程,可以实现绘制图片

2026-01-27 15:25
Z
星小瞳xs
import sensor, image, time, os



# 初始化摄像头

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)


# 加载SD卡中的BMP图片

SD_IMAGE_PATH = "test.bmp"  # SD卡中BMP图片的路径

try:
    bmp_img = image.Image(SD_IMAGE_PATH, sensor.RGB565)
    print("✅ 成功加载图片: %s" % SD_IMAGE_PATH)

except Exception as e:
    print("❌ 加载图片失败: %s" % e)
    while True:
        img = sensor.snapshot()
        img.draw_string(10, 10, "加载图片失败!",color=(255,0,0), scale=2)
        # time.sleep(1)


# 显示图片到IDE缓冲区

while True:

    # 获取摄像头帧缓冲区

    frame = sensor.snapshot()

    # 清除帧缓冲区内容(避免叠加摄像头画面)
    frame.draw_rectangle(0, 0, frame.width(), frame.height(), color=(0,0,0), fill=True)

    # 计算缩放比例,保持图片比例并适配窗口

    x_scale = float(frame.width()) / bmp_img.width()

    y_scale = float(frame.height()) / bmp_img.height()

    scale = min(x_scale, y_scale)

    # 居中绘制图片

    frame.draw_image(bmp_img, (frame.width() - int(bmp_img.width() * scale))//2,

                     (frame.height() - int(bmp_img.height() * scale))//2,

                     x_scale=scale, y_scale=scale)

    # 等待刷新
    # time.sleep(100)