Cropping images in mass


Recently a friend told me:

I have to remove the watermark of hundreds of images, you could do it with a program like GIMP but are hundreds of images, I know that you can automate this process using some commands of the ImageMagick suite; I need your help to make the script in bash.

I told him:

Take it easy that is piece of cake

So here is the procedure

Note: The following procedure has been implemented in Fedora 22 but should work on any GNU/Linux distribution

1. Install the ImageMagick suite

$ sudo dnf -y install ImageMagick

2. Delete watermark

Let's have an image like this:


And we want to remove the watermark (black ribbon). So first we obtain image information using the identify command

$ identify Baker_Supercat.jpg
> Baker_Supercat.jpg JPEG 1024x702 1024x702+0+0 8-bit sRGB 560KB 0.010u 0:00.000

The above command shows that the image has a size of 1024 pixels wide by 702 pixels of height as the black ribbon that you want to remove has 20 pixels of height approximately then execute the convert command as follows. To use

$ convert Baker_Supercat.jpg -crop 1024x682+0+0 cropped_Baker_Supercat.jpg 

The image would look like:


The +0+0 means top left corner of the image if you would like trim 100 pixels from the top of the image then execute the convert command in the following way

$ convert Baker_Supercat.jpg -crop 1024x582+0+100 cropped_Baker_Supercat1.jpg 

The image would look like:


3. Script to automate the process

In the step 2 we saw how to crop an image but what my friend wanted is to do it in mass, so here is the script. The script finds all files jpg, jpeg, png, gif and tiff in the current DIR, calculates the new size of the image and cut out it. The script asks if you want to keep the original file or overwrite it. To use the script download it and give it permissions for execution later copy it in /usr/local/bin then run it in the DIR where the images that you want to trim.

#!/usr/bin/env bash
# GNU shell script to image mass cropping
# --------------------------------------------------------------------------
# Copyleft (Ɔ) Yoander Valdés(sedlav) Rodríguez <>
# This script is released under GNU GPL 2 + licence   
# --------------------------------------------------------------------------
# Use:
# This script find all images under current DIR, calculate new size and crop
# every image. This script asks if you want to keep original file or 
# override it. Download the script give it exec perms and put it on  
# /usr/local/bin.
# --------------------------------------------------------------------------
# Verify if ImageMagick suite is installed
( ! which identify convert &>/dev/null ) && { echo You must install ImageMagick suite; exit 1; }
# Ask for original file keeping
read -p "Do you want to keep original file [y|n]:" answer
# Pattern for jpg, jpeg, png, gif, tiff files
# Find and read every file that match above pattern in current DIR
find $(pwd) -regextype posix-egrep -iregex $PATTERN -type f -print0 | while read -d $'' file; do 
    # Get the image size new
    size=$(identify "$file" | awk '{ print $3 }' | awk -Fx -v to_crop=20 '{ print $1"x"$2-to_crop}')
    # New file name
    cropped_name=$(dirname $file)/cropped_$(basename "$file")
    # Crop the image
    convert "$file" -crop $size+0+0 "$cropped_name"
    # Rename to original name
    [[ $answer =~ ^[0Nn]$ ]] && [[ -e "$cropped_name" ]] && mv -f "$cropped_name" "$file"

You can improve or modify the above script on: Github

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.