# Repeat image.compress does not make image smaller

**URL:** <https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395>\
**Category:** OpenMV Boards\
**Created:** [August 8, 2022, 10:57pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395 "2022-08-08T22:57:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [August 8, 2022, 10:57pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/1 "2022-08-08T22:57:47Z")

</div>

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

```auto
    # 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())

```

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [August 11, 2022, 5:04pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/2 "2022-08-11T17:04:23Z")

</div>

Quality is just a hint to the compression system. Once it reaches the minimum size it can’t get much smaller.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [August 13, 2022, 1:36pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/3 "2022-08-13T13:36:30Z")

</div>

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.

```auto
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.

```auto
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())

```

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [August 14, 2022, 9:04pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/4 "2022-08-14T21:04:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [August 14, 2022, 10:58pm UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/5 "2022-08-14T22:58:11Z")

</div>

How could I achieve compression to a lower quality if the image size is too large?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [August 15, 2022, 12:28am UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/6 "2022-08-15T00:28:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ajacobs](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@ajacobs](https://forums.openmv.io/u/ajacobs)\
**Post date:** [August 24, 2022, 2:22am UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/7 "2022-08-24T02:22:38Z")

</div>

What is to\_jpeg()? I cannot find any documentation for this function.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [August 24, 2022, 5:16am UTC](https://forums.openmv.io/t/repeat-image-compress-does-not-make-image-smaller/7395/8 "2022-08-24T05:16:34Z")

</div>

It’s the same as .compress()

Need to update the docs.
