ImageMagick is a powerful command-line tool for image manipulation, and one of its most versatile features is the composite
command, perfect for layering photos. Whether you need to overlay a logo, create a watermark, or blend images, composite
offers a range of options. This guide will show you How To Layer Photos effectively using different composite
techniques.
Basic Photo Layering: Same Resolution
If you’re working with two images of the same size and simply want to overlay one on top of the other, the basic composite
command is all you need. Let’s say you have a base image named base.png
and an overlay image called overlay.png
, and you want to save the layered result as result.png
.
magick composite overlay.png base.png result.png
This command directly layers overlay.png
onto base.png
, producing result.png
. ImageMagick automatically handles the compositing process, making it straightforward for simple layering tasks.
Positioning Overlays with Gravity
When your base and overlay images have different dimensions, you’ll likely want to control where the overlay is placed. The -gravity
argument lets you specify the position using cardinal directions like north
, south
, east
, west
, northeast
, southeast
, northwest
, southwest
, or center
. For instance, to place your overlay in the top-right corner (northeast) of the base image:
magick composite -gravity northeast overlay.png base.png result.png
The -gravity northeast
option ensures that the overlay.png
is positioned in the northeast corner of base.png
, regardless of their size differences.
Resizing Overlays to Match the Base Image
In scenarios where you need the overlay to scale and fit the dimensions of the base image (maintaining aspect ratio), you can combine resize
with composite
. This is useful when you want an overlay to cover the entire base image proportionally.
magick base.png overlay.png -resize %[fx:u.w]x%[fx:u.h] -composite output.png
Here, -resize %[fx:u.w]x%[fx:u.h]
dynamically resizes overlay.png
to match the width (u.w
) and height (u.h
) of the first image in the command (which is base.png
). The -composite
then layers the resized overlay onto the base, outputting output.png
.
Creating Corner Watermarks with Relative Sizing
For watermarks or subtle overlays that should remain proportionally small relative to the base image size, the -geometry
argument is invaluable. You can define the overlay size as a fraction of the base image dimensions and position it using -gravity
. To create a watermark in the bottom-right corner (southeast) that’s one-eighth the size of the base image:
magick base.png overlay.png -geometry %[fx:u.w/8]x%[fx:u.h/8]+0+0 -gravity southeast -composite output.png
In this command, -geometry %[fx:u.w/8]x%[fx:u.h/8]+0+0
sets the overlay’s width and height to one-eighth of the base image’s width and height, respectively. -gravity southeast
then positions this resized overlay in the bottom-right corner, creating a proportional watermark.
These examples demonstrate the flexibility of ImageMagick’s composite
command for various photo layering tasks, from simple overlays to sophisticated watermarking, all achievable through command-line instructions.