Last updated: September 30, 2024

<span aria-hidden="true" id="automating-image-resizing-and-format-conversion-with-imagemagick"></span>

# Automating image resizing and format conversion with ImageMagick

![Kevin van Zonneveld](/assets/images/teammates/avatar-kvz-4.jpg?dpl=dpl_BY7TrZQKuzkQ5PseorQMAAVfrV9b)

#### Kevin van Zonneveld

Co-founder · Amsterdam, The Netherlands · Show bio

[](https://x.com/kvz)[](https://github.com/kvz)

ImageMagick is a powerful and versatile suite of command-line tools for image processing. Whether you're a developer or a system administrator, ImageMagick enables you to automate common image processing tasks like batch resizing and format conversion efficiently. In this DevTip, we'll explore practical examples that demonstrate how to streamline your workflow using ImageMagick's command-line utilities.

<span aria-hidden="true" id="introduction-to-imagemagick"></span>

## Introduction to ImageMagick

ImageMagick is a cross-platform software suite that allows you to create, edit, compose, and convert bitmap images via command-line tools. Supporting a wide array of image formats and operations, it's ideal for automating image processing tasks and handling large volumes of images. With ImageMagick, you can integrate image processing into your scripts and workflows, saving time and effort.

<span aria-hidden="true" id="setting-up-imagemagick"></span>

## Setting up ImageMagick

Before diving into the commands, let's ensure ImageMagick is installed on your system.

<span aria-hidden="true" id="installing-on-macos"></span>

### Installing on macOS

For macOS users, use Homebrew to install ImageMagick:

```bash
brew install imagemagick

```

<span aria-hidden="true" id="installing-on-linux"></span>

### Installing on Linux

On Debian or Ubuntu-based distributions:

```bash
sudo apt-get install imagemagick

```

On CentOS or Fedora:

```bash
sudo yum install imagemagick

```

On Fedora (newer versions):

```bash
sudo dnf install imagemagick

```

<span aria-hidden="true" id="installing-on-windows"></span>

### Installing on Windows

Windows users can download precompiled binaries from the official ImageMagick website:

1. Visit the [ImageMagick download page⁠](https://imagemagick.org/script/download.php#windows).
2. Choose the appropriate version for your system.
3. Run the installer and follow the prompts.

Ensure you select the option to add ImageMagick to your system path during installation to use the command-line tools from any directory.

<span aria-hidden="true" id="verifying-the-installation"></span>

### Verifying the installation

After installation, verify that ImageMagick is installed correctly:

```bash
magick -version

```

You should see output displaying the installed version and supported features.

###### Note

In ImageMagick 7 and later, the `magick` command is used as a prefix to other commands (e.g., `magick mogrify`). In earlier versions, you can use commands like `mogrify` directly. For consistency, we'll use `magick mogrify` in our examples.

<span aria-hidden="true" id="batch-resizing-images-using-mogrify"></span>

## Batch resizing images using `mogrify`

The `mogrify` command allows you to process multiple images efficiently, making it ideal for batch resizing.

<span aria-hidden="true" id="resizing-images-to-a-specific-width"></span>

### Resizing images to a specific width

To resize all JPEG images in the current directory to a width of 800 pixels while maintaining aspect ratio:

```bash
magick mogrify -resize 800 *.jpg

```

<span aria-hidden="true" id="resizing-images-to-specific-dimensions"></span>

### Resizing images to specific dimensions

To resize images to fit within specific dimensions (e.g., 800x600 pixels):

```bash
magick mogrify -resize 800x600 *.jpg

```

This resizes the images proportionally to fit within the specified dimensions.

###### Note

`mogrify` overwrites the original files. To preserve the originals, process them in a different directory or use `magick convert` instead.

<span aria-hidden="true" id="resizing-and-saving-to-a-different-directory"></span>

### Resizing and saving to a different directory

To resize images and save them to a different directory:

```bash
mkdir resized
magick mogrify -path resized/ -resize 800 *.jpg

```

This saves the resized images to the `resized/` directory, preserving the original files.

<span aria-hidden="true" id="converting-image-formats-in-bulk"></span>

## Converting image formats in bulk

ImageMagick simplifies format conversion with the `mogrify` command.

<span aria-hidden="true" id="converting-jpeg-to-png"></span>

### Converting JPEG to PNG

To convert all JPEG images in the current directory to PNG format:

```bash
magick mogrify -format png *.jpg

```

This creates PNG versions of all JPEG images.

<span aria-hidden="true" id="converting-and-saving-to-a-different-directory"></span>

### Converting and saving to a different directory

To convert images and save them to a different directory:

```bash
mkdir png_images
magick mogrify -format png -path png_images/ *.jpg

```

This converts all JPEG images to PNG and saves them in the `png_images/` directory.

<span aria-hidden="true" id="combining-resizing-and-format-conversion"></span>

## Combining resizing and format conversion

You can combine multiple operations in a single command for efficient batch processing.

<span aria-hidden="true" id="resizing-and-converting-images"></span>

### Resizing and converting images

To resize images and convert them to a different format:

```bash
mkdir resized_png
magick mogrify -resize 800 -format png -path resized_png/ *.jpg

```

This command resizes all JPEG images to 800 pixels in width, converts them to PNG format, and saves them in the `resized_png/` directory.

<span aria-hidden="true" id="automating-tasks-with-shell-scripts"></span>

## Automating tasks with shell scripts

For repetitive tasks, automate the commands using shell scripts to streamline your workflow.

<span aria-hidden="true" id="example-shell-script"></span>

### Example shell script

Create a shell script `process_images.sh`:

```bash
#!/bin/bash

INPUT_DIR="input_images"
OUTPUT_DIR="output_images"
WIDTH=800

mkdir -p "$OUTPUT_DIR"

find "$INPUT_DIR" -type f -name "*.jpg" -exec magick mogrify -resize "$WIDTH" -format png -path "$OUTPUT_DIR" {} +

echo "Images have been resized and converted."

```

Make the script executable:

```bash
chmod +x process_images.sh

```

Run the script:

```bash
./process_images.sh

```

This script finds all JPEG images in `input_images`, resizes them to 800 pixels in width, converts them to PNG, and saves them in `output_images`.

<span aria-hidden="true" id="practical-examples-and-use-cases"></span>

## Practical examples and use cases

<span aria-hidden="true" id="creating-thumbnails"></span>

### Creating thumbnails

To create thumbnails for a website or application:

```bash
mkdir thumbnails
magick mogrify -resize 150x150^ -gravity center -extent 150x150 -format jpg -path thumbnails/ *.jpg

```

This creates square thumbnails of size 150x150 pixels, cropping the images to center.

<span aria-hidden="true" id="optimizing-images-for-the-web"></span>

### Optimizing images for the web

To optimize images for web use by reducing quality:

```bash
mkdir optimized
magick mogrify -quality 85% -path optimized/ *.jpg

```

This reduces the image quality to 85%, resulting in smaller file sizes ideal for web pages.

<span aria-hidden="true" id="conclusion"></span>

## Conclusion

ImageMagick's command-line tools, particularly `mogrify`, offer powerful capabilities for automating image processing tasks such as batch resizing and format conversion. By integrating these commands into your workflow or scripts, you can handle large volumes of images efficiently.

At Transloadit, we leverage ImageMagick in several of our[image processing services](/services/image-processing.md), enabling seamless and scalable image processing in the cloud.

\#imagemagick#image-processing#batch-resizing#format-conversion#command-line-tools#automation#image-processing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
