Resize and Watermark Images in Linux
If you need to resize and watermark large number of images, there is an easy way to do so using Linux command line tools and a very basic shell script. The easiest and most straight-forward method you can use is through the use of the ImageMagick toolkit. If you don’t have it installed already, you can download it from their site. Most distributions have binaries already built for you. Ubuntu and Debian users can install ImageMagick from the main repositories.
Install ImageMagick
sudo apt-get install imagemagick
Prepare your working environment
Make a directory to store the watermark image (~/Pictures/watermark) and photos you’re working on (~/Pictures/temp). Copy the watermark image and original photos to their respective directories.
mkdir -p ~/Pictures/temp/
mkdir -p ~/Pictures/watermark/
cp /path/to/watermark.jpg ~/Pictures/watermark/
cp /path/to/original-photos/*.jpg ~/Pictures/temp/
ls -l ~/Pictures/watermark/
-rw-r--r-- 1 gmendoza gmendoza 3311 2007-12-30 17:35 watermark.jpg
ls -l ~/Pictures/temp
-rw-r--r-- 1 gmendoza gmendoza 885788 2007-12-30 17:35 ubuntu1.jpg
-rw-r--r-- 1 gmendoza gmendoza 128922 2007-12-30 17:31 ubuntu2.jpg
Change directories to begin working on your photos.
cd ~/Pictures/temp/
Resizing with “convert”
Let’s say you want to specify a maximum width of 440 pixels (height being adjusted proportionally) in order for you to post the images appropriately within the specifications of your website borders. The syntax would be the following:
convert -resize 440 original-image.jpg new-image.jpg
In this example, we will specify the same name for the output files, which will overwrite the original copies.
convert -resize 440 ubuntu1.jpg ubuntu1.jpg
convert -resize 440 ubuntu2.jpg ubuntu2.jpg
Watermark with “composite”
You will probably want to watermark the images after they have been resized, this way there is no distortion of the watermark, and it is appropriately sized. In this example, we will also set the watermark transparency level to 15%, and overwrite the original file again.
composite -gravity northeast -dissolve 15 ../watermark/watermark.jpg \
ubuntu1.jpg ubuntu1.jpg
composite -gravity northeast -dissolve 15 ../watermark/watermark.jpg \
ubuntu2.jpg ubuntu2.jpg
Your photos will now have a nice watermark in their upper right hand corners.
Automating the process with a script
You can automate these steps and apply them to a large number of files using a script of course. Feel free to download this one and modify it to your liking.
http://www.savvyadmin.com/downloads/watermark.sh
#!/bin/bash WATERMARK="$HOME/Pictures/watermark/watermark.jpg" echo "*****************************************" echo "* Image Resize and Watermarking Script *" echo "* By Gilbert Mendoza - SavvyAdmin.com! *" echo "*****************************************" echo " " for each in ~/Pictures/temp/*{.jpg,.jpeg,.png} do echo "Working on "$each" ..." convert -resize 440 "$each" "$each" >> /dev/null composite -gravity northeast -dissolve 15.3 $WATERMARK "$each" "$each" >> /dev/null echo "... Done!" done exit 0
Additional Options
Please check out the ImageMagick website for more information on the many options and features their products have to offer.
http://www.imagemagick.org/script/command-line-processing.php
http://www.imagemagick.org/script/command-line-options.php
http://www.imagemagick.org/Usage
Comments
John Biggs said,
Place double quotes around the $each variables to handle files with spaces in the name.
Example:
convert -resize 440 “$each” “$each” 2> /dev/null
Подготовка фотографий к публикации в интернете. « Блог про Linux said,
[...] в считанные минуты справиться с этой задачей. Вот тут предлагается решение этой задачи. Это пример того [...]
blastfm said,
thank you for this… really big help for me
Подготовка фотографий к публикации в интернете. | von@darkstar:~$ cat /dev/random >> blog said,
[...] в считанные минуты справиться с этой задачей. Вот тут предлагается решение этой задачи. Это пример того [...]
Rahul said,
What a waste of the authors time! A simple image displaying the watermark would have helped a reader judge the outcome. I have to skip this article sorry.
Georgi Georgiev - hip0 said,
Nice artice. I’ve just written an article on watermarking pictures that explains a bit more details on watermarking and might be helpful to people. – http://www.pc-freak.net/blog/how-to-place-digitally-watermark-a-picture-to-protect-it-with-copyright-image-or-text-with-composite-convert-and-phatch-on-linux-watermark-pictures-to-prevent-your-copyright-infringement/ – The script is quite helpful. Thx
Add A Comment