Quick Links

ImageMagick is a powerful Linux command line tool for batch editing images. Here’s how to use it to crop, resize, and merge photos, as well as remove location information.

First, Install ImageMagick

Batch editing photos in Linux can be done using the ImageMagick program. ImageMagick is a free tool for creating and editing images from the command line, and can resize, crop, convert, rotate, and perform other bulk image actions.

The Linux terminal command examples on this page all use ImageMagick, which you can install on Debian/Ubuntu systems by running:

sudo apt install imagemagick

Or on Fedora Linux by running:

sudo dnf install ImageMagick

You should check what the terminal commands you copy/paste from the internet do before you run them! While I’ve explained the ones in this article, commands can have unintended effects on different systems, and people have been known to post harmless-looking commands online that can actually do a lot of damage.

Remove Ads

Batch Remove EXIF Data From Images

EXIF data contains information about the device that was used to take a photo, color information, and other data that is useful for photographers. It can also include the location the photo was taken, a privacy concern if you’re uploading the image to the internet.

You can remove all the EXIF data from a batch of JPG and PNG images using the following Linux terminal command:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -strip "/path/to/output_directory/$(basename "$file")"; done

Here’s a quick explanation of how this command (and the others on this page) perform bulk editing on multiple image files:

  • A for loop iterates over all the .jpg, .jpeg, and .png (note that file names and extensions are case-sensitive!) in the source directory. Within the loop, the path to each image file is assigned to the $file variable.
  • The $file variable is passed to the ImageMagick convert command with the -strip option that tells it to strip all EXIF data.
  • The final parameter passed to the convert command is the output file path. In this case, the output is saved to a different folder with the same file name, so your original image is left intact.
Remove Ads

Batch Resize Photos

The below ImageMagick terminal command resizes all the images in the source directory to 1024×768 pixels using the -resize option:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -resize 1024x768 "/path/to/output_directory/$(basename "$file")"; done

You can specify any resolution here, but keep in mind your image will be stretched if the aspect ratio doesn’t match.

Batch Crop Photos

This command batch crops images in a directory to 800×600 pixels:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -crop 800x600+0+0 "/path/to/output_directory/$(basename "$file")"; done

Note the 800×600+0+0 passed to the -crop option: the 800×600 bit is the pixel dimensions you want to crop (the area of the photo you want to keep). The +0+0 is the X/Y pixel coordinates of where the crop should start, in this case, starting in the top-left corner.

Batch Watermark Images

Watermarking images is an easy way to protect your creative work when you share it with others. This Linux terminal command adds a watermark to a batch of files:

Remove Ads
for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -gravity southeast -pointsize 20 -draw "text 5,5 'My Watermark'" "/path/to/output_directory/$(basename "$file")"; done

In this example, the convert command uses the -gravity option in conjunction with the -draw option to draw the text “My watermark” with a font -pointsize of 20 in the bottom-right (southeast corner) of each image. The 5,5 pixel coordinates specify the offset from the southeast corner, adding a 5 pixel margin between the watermark text and the edge of the image.

Convert Image File Formats

This ImageMagick command batch converts JPG images to PNG:

for file in /path/to/source_directory/*.{jpg,jpeg}; do convert "$file" "/path/to/output_directory/$(basename "${file%.*}.png")"; done

The format of the output image is specified by the file name you give it. The ImageMagick convert command supports the following popular image file formats: JPEG, PNG, GIV, TIFF, BMP, SVG, WEBP, HEIC, and RAW. It can also handle PDF files and SVG vector images.

Remove Ads

Rotate Images in Bulk

You can bulk-rotate images using the following command:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -rotate 90 "/path/to/output_directory/$(basename "$file")"; done

Replace the 90 degrees specified with the rotation you wish to apply.

Batch Flip (Mirror) Images

Use the flip and flop ImageMagick convert options to flip images vertically or horizontally:

To bulk flip, or mirror, images vertically:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -flip "/path/to/output_directory/$(basename "$file")"; done

To bulk flip images horizontally:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -flop "/path/to/output_directory/$(basename "$file")"; done

Change JPEG Quality

You can reduce the size of JPG images by reducing their quality:

for file in /path/to/source_directory/*.jpg; do convert "$file" -quality 85 "/path/to/output_directory/$(basename "$file")"; done

This command sets the quality of the images to 85 using the -quality option. Note that when you reduce the quality of an image, that quality can’t be restored (even by changing the quality back to a higher value).

Remove Ads

JPEG quality is set on a scale of 0-100, with 100 being the maximum quality for archival storage and professional photography, and 60-75 being suitable for online sharing or for use on websites. Anything under 50 will start to look a bit shabby, even at the default zoom level.

Rename Images in Bulk

The cp (copy) command can be used to bulk rename images in Linux:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do cp "$file" "/path/to/output_directory/$(basename "${file%.*}_renamed.${file##*.}")"; done

This example appends the text _renamed to all the images in the source directory.

Auto Crop and Trim

The -trim option for the convert command automatically crops images based on the background color, determined by the color of the pixels in each of the images’ corners:

for file in /path/to/source_directory/*.{jpg,jpeg,png}; do convert "$file" -trim "/path/to/output_directory/$(basename "$file")"; done

Merge Images to Create Collages

The montage ImageMagick tool can be used to create collages and merge images:

Remove Ads
montage /path/to/source_directory/*.{jpg,jpeg,png} -tile 2x2 -geometry +5+5 "/path/to/output_directory/collage.jpg

This command takes all the images in the source directory and creates a 2×2 grid with them with the -tile option, with a 5 pixel gap specified by the -geometry option. If there are more than 4 images, additional grids will be created to display them.

Linux ❤️ Creativity

Linux is becoming increasingly popular with creatives. Not only has its creativity software reached the point where it can be used in professional scenarios, its command line and automation tools can save you time managing and converting your images for publishing and sharing.