img.draw_image(img0)绘制显示全黑

Viewed 68

问题描述


想在tmp_buf临时画布画imread的图片(确认图片没问题)
imread读完图片转为numpy数组后display.show_image可以正常显示图片,说明图片读取正常、转为数组正常
但在临时画布tmp_buf.draw_image(test_img, 0, 0)绘制后,屏幕显示tmp_buf显示全黑
没有报错、画面全黑页看不到没有testimg图片
代码如下:

import cv2
from ulab import numpy as np
import image
from media.display import *
import time
Display.init(Display.ST7701, width=800, height=480, to_ide=True)
def np_to_image(np_arr):
    """将BGR三通道uint8 numpy数组转ARGB8888 image.Image,修复通道错位黑屏"""
    if len(np_arr.shape) != 3 or np_arr.shape[2] != 3:
        print("np数组维度错误,必须为H,W,3 BGR")
        return None
    if np_arr.dtype != np.uint8:
        np_arr = np_arr.astype(np.uint8)
    # 强制拷贝,防止局部numpy被GC回收导致画布空白
    np_arr = np_arr.copy()
    h, w = np_arr.shape[:2]
    img_obj = image.Image(w, h, image.ARGB8888)
    dst_np = img_obj.to_numpy_ref()
    # ARGB8888 存储顺序 [A, R, G, B]
    dst_np[:, :, 0] = 255              # Alpha 不透明
    dst_np[:, :, 1] = np_arr[:, :, 2]  # R = cv2 BGR第三通道
    dst_np[:, :, 2] = np_arr[:, :, 1]  # G = cv2 BGR第二通道
    dst_np[:, :, 3] = np_arr[:, :, 0]  # B = cv2 BGR第一通道
    return img_obj
img_np = cv2.imread("/data/video/pic-2026-07-2-11-14-57.jpg")
resized_np = cv2.resize(img_np, (320, 240)).copy()
test_img = np_to_image(resized_np)

# 创建临时画布
tmp_buf = image.Image(800,480,image.ARGB8888)
tmp_buf.clear()
# 绘制
tmp_buf.draw_image(test_img, 0, 0)
# 显示画布
Display.show_image(test_img)#test_img可正常显示,tmp_buf画面全黑
time.sleep_ms(1000)

复现步骤


1、准备一张jpg图片
2、修改代码指定图片路径
3、CanMV执行代码
4、在Display.show_image(test_img)可以切换test_img可正常显示,tmp_buf画面全黑

硬件板卡


01Studio CanMV K230

软件版本


CanMV_K230_01Studio_micropython_v1.6-50-g297b646_nncase_v2.11.0

1 Answers

你好,Image模块直接可以加载jpeg图像。

你好,我想要读指定路径下的图片集显示缩略图,所以一次要读多张图片,Image模块读jpeg图像会报错Out of fast frame buffer stack memory,如果想用cv2读图并转numpy后draw_image绘制在Image.image画布上就会显示不出来,这是什么原因,怎样能实现?

多大的图片?

640*480