How to reduce the size of an image while keeping the content

How to reduce the size of an image while keeping the content

There are many ways to reduce the size in kb of an image reducing its quality, but what I am going to explain today has nothing to do with KB and s with size in pixels, width and height, something that is usually done by cutting the content to adjust to needs.

If we have a photograph that is 1700px wide and we need to put it in a place that only accepts 1300px, we will have two options:

– Change the scale by modifying height and width to keep all the content, something that can be done with solutions like the one offered in this link (click here). – Cut the image sacrificing part of the content, something that in many cases is not acceptable. This famous crop can be done with sites like this other link (click here).

The fact is that now we have a third option: reduce the width and length but without respecting proportions and maintaining the content, something only possible with Artificial Intelligence and only valid if the image has a lot of empty space, like the one in the upper capture.

This solution can be tested at trekhleb.dev/js-image-carver. There we have several balloons with a lot of blue sky in the background. When deciding the new size, we see how the system gathers the balloons together to adjust to the requested size.

I have tested it with many other photos, and the result is excellent most of the time (as long as it maintains the characteristics indicated above).

Seam Carving algorithm

The project has been created with the Seam Carving algorithm. The author of the same indicates that the resizing of the image depending on the content can be applied when it comes to changing the proportions of the image (that is, reducing the width while maintaining the height) and when it is not desirable to lose some parts of the image. the picture. In this case, making the image scale simple will distort the objects it contains. To preserve the proportions of the objects while changing the proportions of the image, we can use the Seam Carving algorithm that was introduced by Shai Avidan and Ariel Shamir.

The idea of ​​the Seam Carving algorithm is to find the continuous sequence of pixels with the least contribution to the image content and then cut (remove) it. This process is repeated over and over again until we get the required image width or height.