Resize and watermark images in Ubuntu

In this post i am going to show you how to resize and watermark images in bulk for free, without additional software and through bash script that will work on majority of linux distributions. This is very useful for anyone who has to regularly resize and watermark pictures for websites or catalogues.

You will need ImageMagick, most linux distributions already have it installed.

sudo apt-get install imagemagick

If it is already installed, you should see this:

Reading package lists... Done

Building dependency tree

Reading state information... Done

imagemagick is already the newest version.

What is ImageMagick

It allows you create, edit and display images from command line.

It can read, convert and write images in a large variety of formats. Images can be cropped, colors can be changed, various effects can be applied, images can be rotated and combined, and text, lines, polygons, ellipses and B├ęzier curves can be added to images and stretched and rotated.

(source: https://help.ubuntu.com/community/ImageMagick)

Shell script

Create a file called “watermark.sh” and open it in your favourite editor.

The first line we want to add is

#!/bin/bash

This means that the script should be run in the bash shell.

WATERMARK="watermark.png"

resize=450

These are our global variables.

# "*****************************************"

# "* Image Resize and Watermarking Script *"

# "* By Krutant.com *"

# "*****************************************"

The above is the disclaimer, please keep this in the file.

read -p "Watermark with file \""$WATERMARK"\" & resize all images to width "$resize"?  " prompt

Prompt user of the filename used to watermark and the size.

if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]

then

If the user keys in y or yes then

for each in *{.jpg,.jpeg,.JPG,.JPEG}

do

for each jepg or jpeg file

echo -n "Working on "$each" ..."

Output the file name to be resized and watermarked.

convert -resize $resize "$each" "$each" >> /dev/null

Convert command to resize the file to the width specified.

composite -gravity center -dissolve 100 $WATERMARK "$each" "$each" >> /dev/null

Composite command to place the watermark in center of the image.

echo "[Done]"

done

echo ""

read -p "Press Enter to exit ..."

else

exit 0

fi

Above is the end section where we output “done”, wait for user to press enter before exiting.

Save it to your home directory, right click on the file, click properties, permissions tab and tick “Allow executing file as a program”

or type this in terminal

sudo chmod a+x ~/watermark.sh

To run it, copy watermark.sh and watermark.png file into any folder with images and then double click and select “run in terminal” or open terminal and type “./pathtofile/watermark.sh”

When you run it, you will see few errors because the script will try to resize and watermark file name “.jpg”. To fix this, add below line just above the “for each loop” to ignore nulls.

shopt -s nullglob

Thats it! This is very simple script but it does the job. There is alot of room for improvement such as adding more image types, ignoring the watermark.png file so it does not resize the actual watermark file and so on. The main commands are convert and composite, typing them in terminal will bring up many other options.

Here is the complete code:

#!/bin/bash

WATERMARK="watermark.png"

resize=450

# "*****************************************"

# "* Image Resize and Watermarking Script *"

# "* By Krutant.com *"

# "*****************************************"


read -p "Watermark with file \""$WATERMARK"\" & resize all images to width "$resize"? " prompt

if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]

then

echo ""

shopt -s nullglob

for each in *{.jpg,.jpeg,.JPG,.JPEG}

do

echo -n "Working on "$each" ..."

convert -resize $resize "$each" "$each" >> /dev/null

composite -gravity center -dissolve 100 $WATERMARK "$each" "$each" >> /dev/null

echo "[Done]"

done

echo ""

read -p "Press Enter to exit ..."

else

exit 0

fi
 

Batch resize and rotate with Nautilus

In Terminal:

sudo apt-get install nautilus-image-converter

Once it is installed, restart your computer or restart Gnome

sudo /etc/init.d/gdm restart

Browse to the folder where you have your photos, right click the photos and you will see the options to “Resize Images” and “Rotate Images”. Once you select resize or rotate option, You will get options to perform the operations.

Leave a Reply

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