I am compressing an image to jpg to transmit over uart. If the image is too large, I would like to compress with a lower quality. However, the second image.compress with quality=5 does not reduce the image size. In fact, the image size stays the same as the first compress. With the code below, the serial terminal output is:
Uart: Image Size qual=50 9372
Uart: Image Size qual=5 9372
Uart: ERROR Image Too Big 9372
# Uart Send Image
UART_TX_MAX = 1024*9
img_uart = camera.img.compress(quality=50)
print("Uart: Image Size qual=50", img_uart.size())
if (img_uart.size() <= UART_TX_MAX):
TxUartPacketArray(CMD_IMAGE, img_uart)
else:
img_uart = camera.img.compress(quality=5)
print("Uart: Image Size qual=5", img_uart.size())
if (img_uart.size() <= UART_TX_MAX):
TxUartPacketArray(CMD_IMAGE, img_uart)
else:
print("Uart: ERROR Image Too Big ", img_uart.size())
Quality is just a hint to the compression system. Once it reaches the minimum size it can’t get much smaller.
It has not reached the minimum size. Here is some code that demonstrates the issue.
With Quality = 50, the image size is 5100-5200, When Quality = 5 the image size is 2800-2900.
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
img_num = 0
while(True):
img_num += 1
print("Image #", img_num)
img = sensor.snapshot()
# select quality = 50 or quality = 5
img_compress = img.compress(quality=50)
# img_compress = img.compress(quality=5)
print("Image Size", img_compress.size())
The following code shows that when the image compression is performed at quality = 50, and then repeated at quality = 5, the image size is not reduced. It image size for quality = 5 is exactly the same size as the image size for quality = 50.
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
img_num = 0
while(True):
img_num += 1
print("Image #", img_num)
img = sensor.snapshot()
img_compress = img.compress(quality=50)
print("Image Size quality = 50:", img_compress.size())
img_compress = img.compress(quality=5)
print("Image Size quality = 5:", img_compress.size())
Yes, because the image is already a jpeg image and can’t be compressed again.
compress is just silently failing since you passed it a jpeg to compress. It doesn’t uncompress the image and then recompress it.
How could I achieve compression to a lower quality if the image size is too large?
to_rgb565() and then to_jpeg() again.
Please make a bug on the issues page about this and I’ll fix it. This is like 3 lines to add to the source code.
I’m putting time on OpenMV again now.
What is to_jpeg()? I cannot find any documentation for this function.
It’s the same as .compress()
Need to update the docs.