imagemagick cookbook
Watermark an image
First, create a blank image that will serve as a canvas for the watermark:
convert -size 900x15 xc:white filename.ext
Then, print a custom text to the previously created blank image:
composite label:'label-here' input.ext output.ext
Finally, apply (append) the watermark as desired.
To a single image:
convert input.ext watermark.ext -append output.ext
To a series of images:
Use a for
loop:
for img in image-name-**.ext; do
convert $img watermark.ext -append $img
done
Where img
is the chosen attribute name, and *
is a wildcard character used to match files with the same naming pattern.
Notes:
- Being
$img
set as input and output, the original images will be overwritten by the watermarked ones. - This loop uses the bash syntax
- The loop can also be used in single-line form
for img in image-name-**.ext; do convert $img watermark.ext -append $img; done
Crop an image
Use the cli command convert filename.ext -crop 000x000+00+00 croppedfilename.ext
, where 000x000
is the desired size of the cropped image and +00+00
specifies the coordinates of the cropping point; note that the offset position of the "-crop" by default is relative to the top-left corner of the image.
Examples:
# Crop an image:
convert input-image.jpg -crop 1488x963+96+29 output-image.jpg
# Crop all the .jpg images in the working directory:
convert *.jpg -crop 1488x963+96+29 *.jpg
# Crop an image relative to its center:
convert input.jpg -gravity Center -crop 00.00%x000+0+0 output.jpg
# Where 00.00% defines the amount of image width that will be cropped, expressed in percentage
Resize an image
There are different methods:
By dictating the total desired pixel area count:
convert *.jpg -resize 460800@ *.jpg
Fixed height, proportional width:
convert -resize x480 *.jpg *.jpg
Fixed width, proportional height:
convert -resize 960x *.jpg *.jpg
Perform batch operations in Imagemagick
Use a for loop, for example:
for file in ./*;
do convert $file -gravity Center -crop 89.35%x420+0+0 output-dir/`basename $file`;
done
This loop crops files in the current directory and saves the output to a previously created output-dir
directory.
Append images
There are different methods, based on the desidered result:
For merging 2 images into 1:
convert input-1.ext input-2.ext -append output.ext
For batch processing:
convert *.png -append output.png