Last updated: February 5, 2025

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

# Batch image resizing and 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)

In web and application development, efficient image handling is crucial for both performance and user experience. Whether you're working on a personal project or managing a large-scale application, automating image processing tasks can save you valuable time and ensure consistency. In this DevTip, we'll explore how to use ImageMagick's powerful command-line tools for batch resizing and format conversion, enabling you to process multiple images quickly and reliably.

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

## Introduction to ImageMagick

ImageMagick is a versatile, open source software suite for displaying, converting, and editing raster image files. Widely adopted in the developer community, it supports over 200 image formats and provides robust command-line tools for performing complex image manipulations—from simple resizing to advanced effects.

###### Note

This guide uses ImageMagick 7 syntax. If you're using ImageMagick 6 (still common in many Linux distributions), you can omit the `magick` prefix from commands. Check your version with`convert -version` or `magick -version`.

###### Important

It is strongly recommended to establish a security policy suitable for your local environment before utilizing ImageMagick. See the[security policy documentation⁠](https://imagemagick.org/script/security-policy.php) for details.

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

## Setting up ImageMagick

Before diving into the commands, you must install ImageMagick on your system. The installation process varies depending on your operating system.

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

### Installing on macOS

For macOS users, install ImageMagick using Homebrew:

```shell
brew install imagemagick

```

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

### Installing on Linux

On Ubuntu or other Debian-based distributions:

```shell
sudo apt-get update
sudo apt-get install imagemagick

```

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

### Installing on Windows

For Windows users, you have several options. You can download the installer from the[official ImageMagick website⁠](https://imagemagick.org/script/download.php#windows) or use a package manager:

```shell
winget install ImageMagick.ImageMagick
# Or
choco install imagemagick
# Or
scoop install main/imagemagick

```

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

## Batch resizing images using ImageMagick

<span aria-hidden="true" id="how-do-i-resize-multiple-images-at-once-using-imagemagick"></span>

### How do I resize multiple images at once using ImageMagick?

To resize multiple images simultaneously, use the `mogrify` command, which is designed for bulk image processing. Unlike `convert`, which writes the output to a different file, `mogrify` modifies the original files. It is advisable to work on copies or back up your images before processing.

<span aria-hidden="true" id="resizing-images-in-bulk"></span>

#### Resizing images in bulk

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

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

```

This command processes all `.jpg` files in the current directory, resizing them to a width of 800 pixels. The height is automatically adjusted to maintain the original aspect ratio.

<span aria-hidden="true" id="preserving-original-files"></span>

#### Preserving original files

To keep the original images intact and save the resized versions in a separate directory:

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

```

This command creates a `resized` directory and stores the resized images there, leaving the originals untouched.

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

## Converting image formats in bulk

<span aria-hidden="true" id="how-can-i-convert-images-from-one-format-to-another-using-imagemagick"></span>

### How can I convert images from one format to another using ImageMagick?

ImageMagick makes converting images from one format to another straightforward using the `mogrify`command. Note that some formats may require additional libraries to be installed.

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

#### Converting JPEG to PNG

To convert all JPEG images in a directory to PNG format:

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

```

This command creates new `.png` files while preserving the original `.jpg` files, allowing for easy comparison or backup.

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

## Combining resizing and format conversion

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

### Combining resizing and format conversion in a single command

You can combine multiple operations in one command to process your images more efficiently, saving time and reducing workflow steps.

<span aria-hidden="true" id="resize-and-convert-simultaneously"></span>

#### Resize and convert simultaneously

To resize images and convert them to a different format in one go:

```shell
magick mogrify -resize 800 -format png *.jpg

```

This command resizes all `.jpg` images to a width of 800 pixels and saves them as `.png` files, handling both operations in a single pass.

<span aria-hidden="true" id="automating-image-processing-tasks"></span>

## Automating image processing tasks

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

### Automating tasks with shell scripts

For repetitive tasks or complex workflows, automate the process using shell scripts. This not only speeds up your workflow but also reduces the chance of errors.

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

#### Sample shell script

Create a script named `batch_process.sh`:

```shell
#!/bin/bash

# Create directories for outputs
mkdir -p resized/png

# Resize images and save in the 'resized' directory
magick mogrify -path resized -resize 800 *.jpg

# Convert the resized images to PNG format and save in a subdirectory
magick mogrify -path resized/png -format png resized/*.jpg

```

Make the script executable:

```shell
chmod +x batch_process.sh

```

Run the script:

```shell
./batch_process.sh

```

This script resizes all `.jpg` images to a width of 800 pixels, saves them in the `resized`directory, and then converts those images to `.png` format in the `resized/png` directory, demonstrating a practical automation workflow.

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

## Practical examples and use cases

* **Preparing Images for Web Use**: Optimize images for faster load times by resizing and converting them to web-friendly formats, enhancing your site's performance.
* **Standardizing Image Assets**: Ensure that all images in a project meet specific dimensions and formats, maintaining consistency across your application or website.
* **Bulk Processing Scanned Documents**: Convert and resize scanned images for uniform archival storage, streamlining document management.
* **Creating Thumbnails**: Automatically generate thumbnails for image galleries or product catalogs, improving the visual experience for users.

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

## Conclusion

Automating image processing tasks with ImageMagick can significantly enhance your productivity as a developer. By leveraging command-line tools such as `mogrify`, you can efficiently batch resize and convert images, streamlining your workflow and ensuring consistency across your projects.

At Transloadit, we integrate ImageMagick within our Image Processing Robots—such as the[Image Resize Robot](/docs/robots/image-resize.md)—to provide robust and scalable image processing solutions. If you're looking to automate image tasks at scale, consider exploring what Transloadit has to offer.

\#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
