How to Change Image DPI with ImageMagick Convert?
Changing the DPI (dots per inch) or resolution of an image using
ImageMagick’s convert command is a straightforward process
that involves modifying the image’s metadata or resubmitting its pixel
dimensions. This article provides a quick overview of how to use the
-density and -resample flags to alter image
resolution for both print and digital display, ensuring your images meet
specific quality requirements without losing clarity.
Understanding Density vs. Resampling
When altering image resolution in ImageMagick, you must choose between changing the metadata (density) or changing the actual pixel count (resampling).
- The
-densityflag: This option changes the DPI metadata of the image without altering the actual pixel dimensions. It tells a printer how many pixels to squeeze into an inch, which is ideal when you need to meet specific print requirements (like 300 DPI) without degrading the original image quality. - The
-resampleflag: This option changes both the DPI metadata and the actual pixel dimensions of the image. If you increase the DPI, ImageMagick will add pixels (upsampling); if you decrease it, ImageMagick will remove pixels (downsampling) to maintain the visual size at the new resolution.
How to Change DPI Metadata Only
If you need to change the DPI for print purposes but want to keep the
exact same pixel dimensions, use the -density flag followed
by your desired resolution. You should also include the
-units flag to specify pixels per inch.
convert input.jpg -units PixelsPerInch -density 300 output.jpgIn this example, input.jpg is converted to
output.jpg with a metadata setting of 300 DPI, while the
underlying pixel grid remains completely untouched.
How to Resample the Image Resolution
If you want to change the DPI and have ImageMagick automatically
resize the pixel dimensions of the image to match that new resolution,
use the -resample flag.
convert input.jpg -units PixelsPerInch -resample 72 output.jpgThis command is highly useful when optimizing large, high-resolution print images for web display by downsampling them to standard screen resolutions like 72 DPI, which significantly reduces the file size.