Trying to create a reference picture on the fly

Hello to all,

i’ll try to create a white reference picture on the fly on my H7 camera.

Therefore i create a extra framebuffer and draw a white rectangle in the framebuffer.

For debugging purpose i’d like to show the extra frame buffer in the IDE.

Here is my code snipped:

print(int(x),int(y),int(width),int(height))

#create reference picture white
print(sensor.width())
print(sensor.height())
img1 = sensor.alloc_extra_fb(sensor.width(),sensor.height(), sensor.RGB565)
#img1 = sensor.alloc_extra_fb(xstep, ystep, sensor.RGB565)
#img1 = image.Image(xstep,ystep,sensor.BINARY,True)
#img1.replace(sensor.snapshot())
img1.clear()
img1.crop(roi=(0, 0, int(width),int(height)))
img1.draw_rectangle(0,0,int(width),int(height),color=(255,255,255),fill=True)

i = 0

while(True):
    i += 1
    print(img1.compressed_for_ide(), end="")
    print(i)
    utime.sleep_ms(100)

When i try to show the frame buffer with the line:

print(img1.compress_for_ide(), end="")

The IDE crashes with the error message:
“OSError: The new image won’t fit in the target buffer!”

Why is the buffer not large enough? The rectangle ist width = 548 and height = 325.

The memory is allocated with 800 x 600.

When i change it to the line mentioned above

print(img1.compressed_for_ide(), end="")

the while loop does 7 till 8 iterations, then crashes with:
“MemoryError: memory allocation failed, allocation xxx bytes”

what do i wrong? Have to be the memory not allocated only once?

Is there a easier method to create a plain reference picture on the fly?

Thanks in advantage.

Hi, I need to re-work this part of the API. Anyway:

print(img1.compressed_for_ide(), end="")

This generates a compressed copy of the image on the heap and then sends it to the IDE.

print(img1.compress_for_ide(), end="")

This compresses the image in place and then sends it to the IDE. However, after calling it once the original frame buffer is destroyed. When you call it again that’s when it fails. You’d need to reset the frame buffer each time.

What the compress_for_ide() function does is jpeg compress and image and then encode it with such that 6 bits map to 8 bits in order to prevent the image from intersecting with other ASCII characters when it’s sent to the IDE. We don’t mark that this happened however. Only the jpeg compression part. So, when you call it again on the same image it does this expansion again, and again, and again until the image doesn’t fit. Note that the image is corrupted after the second time this happens…