Smart Image Cropping with katna

Some of the simple tasks for humans can be very tedious for machines. Image cropping is one of them. It's quite easy for human to identify the important area in a photo and decide the parts to retain or crop. But for machines, it is quite difficult.

Katna is a python based open source smart image cropping tool that can intelligently identify important elements of an image and retain it during cropping. Katna content — aware algorithm can also retain important texts in the image.

The best way to learn about Katna smart cropping module is to actually use it.


Katna can be installed either via PyPI or directly from source.

PyPIInstallation via PyPI is pretty straight forward

pip install katna

Install from source Follow the steps below

git clone

Change the current working directory to the folder where you have cloned Katna repo.

cd <<path_to_the_folder_repo_cloned>>

If you use the anaconda python distribution then create a new conda environment. Keeping environment separate is a good practice.

conda create --name katna python=3
source activate katna

Run the setup

python install

How to use katna image module

Import the video module from the katna library

from Katna.image import Image

Instantiate the image class.

img_module = Image()

Image class offers different cropping methods for different inputs. here is a quick list of different cropping methods

Let us look at the function definitions now.

crop_image — . This method accepts 6 parameters and returns a list of images as numpy 2D array. Below are the six parameters of the function.

# number of images to be returned
no_of_crops = 3

# crop dimensions
crop_width = 1000
crop_height = 600

# Filters
filters = ["text"]
sampling_factor = 12image_file_path = <Path where the image is stored>

crop_list = img_module.crop_image(
num_of_crops= no_of_crops,
down_sample_factor = sampling_factor

crop_image_from_cvimage — It accepts opencv image as image source, rest of the parameters are same as crop_image function.

crop_image_with_aspect — It accepts the aspect ratio for cropping dimension. Rest of the parameters are same as crop_image

How katna image module works

All possible crops from the input image for the crop size is selected and passed through set of filters — The rule of third, Saliency, face detection and edge detection. Images shown below are filter output of an image. Each of the filter gives a score to the crops. If the text retention filter is switched on then it filters out crops that cuts the text. The final emerging crops are sorted and then numbers of crops requested by the caller are returned.

What’s next

We plan to add more filters in future like violence, nudity etc. to make it more robust.

We are thankful to open source community and project smartcrop especially that enabled us to reuse and the take good ideas forward. If you find the tool useful please do share the project.

That’s it!! You can find a complete application here.

Certified Solution architect | Experience technologist | Speaker | Developer. Views and opinions are personal