user
On Ubuntu,how to resize several images on command line ?
alphonsio

On Ubuntu, you can use the mogrify command from the package imagemagick to resize several images:

sudo apt install imagemagick
mogrify -resize 800x600! *.jpg

Note that the mogrify command modifies the images in place.


To resize several images on the command line in Ubuntu, you can use ImageMagick, a powerful tool for image manipulation. Here are the steps to install ImageMagick and resize multiple images:

Install ImageMagick

First, you need to install ImageMagick if you haven't already:

sudo apt update
sudo apt install imagemagick

Resize Multiple Images

You can use the mogrify command, which is a part of ImageMagick, to resize multiple images. The mogrify command modifies the images in place, so it's a good idea to make a backup of your images or work on copies.

Here are the steps to resize multiple images:

  1. Navigate to the directory containing your images:
cd /path/to/your/images
  1. Resize all images in the current directory:

    For example, to resize all .jpg images so that their width is 800 pixels while maintaining the aspect ratio, you can use:

mogrify -resize 800x *.jpg
- If you want to ensure the height is 800 pixels instead of width, you can use:
mogrify -resize x800 *.jpg
- To set both dimensions (which could change the aspect ratio), you can specify both width and height:
mogrify -resize 800x600! *.jpg
  1. Resize and save to another directory to avoid overwriting:

    If you prefer to save the resized images to a different directory without overwriting the originals, you can use convert in a loop. Here is an example using a simple bash loop:

mkdir -p resized
for img in *.jpg; do
    convert "$img" -resize 800x "resized/$img"
done

Example: Resize images to a specific percentage

To resize all .jpg images in the directory to 50% of their original size:

mogrify -resize 50% *.jpg

Example: Resize images using a script

If you have a directory structure and you want to resize images in multiple directories, you might want to use a script:

#!/bin/bash
SRC_DIR="/path/to/your/source"
DEST_DIR="/path/to/your/destination"
SIZE="800x"

mkdir -p "$DEST_DIR"

find "$SRC_DIR" -type f -name "*.jpg" | while read FILE; do
    DEST_FILE="$DEST_DIR/$(basename "$FILE")"
    convert "$FILE" -resize "$SIZE" "$DEST_FILE"
done

Save the script as resize_images.sh, make it executable, and run it:

chmod +x resize_images.sh
./resize_images.sh

This script will find all .jpg files in the source directory and its subdirectories, resize them, and save the resized versions in the destination directory, maintaining the directory structure.

These methods should help you resize multiple images on the command line efficiently.