Jérôme
Jérôme

Reputation: 2720

Resize a list of images in line command

I would like to resize a list of images, all in the directory. To achieve that, I use convert from imagemagick. I would like to resize

image1.jpg
image2.jpg
...

into

image1-resized.jpg
image2-resized.jpg
...

I was wondering if there is a method to achieve this in a single command line. An elegant solution could be often useful, not only in this case.

EDIT:

I would like a non script-like solution, ie. without for loop.

Upvotes: 8

Views: 8987

Answers (4)

DaveMan
DaveMan

Reputation: 600

GNU Parallel is even easier than for loops, and it's often faster:

parallel convert -resize 800x600 -- "{}" "{.}-resized.jpg" ::: *.jpg

A few things going on here, from right to left:

  • ::: *.jpg means run the command for every jpg file
  • {.} means insert the current filename without the suffix (.jpg)
  • {} means insert the current filename
  • parallel means run the following command many times in parallel. It will choose the max to do in parallel to match the number of cores your computer has. As each one finishes it will launch the next one until all the jpg files are converted.

This runs the command convert --resize 800x600 -- foo.jpg foo-resized.jpg for each file. The -- tells convert to stop processing flags, in case a file name happens to start with a -.

P.S. On my mac I have Homebrew installed, so I was able to install parallel and convert with

brew install parallel
brew install imagemagick

Upvotes: 6

chaos
chaos

Reputation: 124287

ls *.jpg|sed -e 's/\..*//'|xargs -I X convert X.jpg whatever-options X-resized.jpg

You can eliminate the sed and be extension-generic if you're willing to accept a slightly different final filename, 'resized-image1.jpg' instead of 'image1-resized.jpg':

ls|xargs -I X convert X whatever-options resized-X

Upvotes: 8

Nietzche-jou
Nietzche-jou

Reputation: 14720

If your image files have different extensions:

for f in *; do convert -resize 800x600 -- "$f" "${f%.*}-resized.${f##*.}"; done

Upvotes: 2

Johannes Weiss
Johannes Weiss

Reputation: 54031

If you want to resize them to 800x600:

for file in *.jpg; do convert -resize 800x600 -- "$file" "${file%%.jpg}-resized.jpg"; done

(works in bash)

Upvotes: 10

Related Questions