34

I am using ImageMagick convert command for making thumbnails & save the converted images in another directory, one by one, in PHP.

But, cant figure out how to keep the the image name in the converted image.

> convert 1.JPG -resize 120X120 thumb/*.JPG

need to keep the output file names same as the input. Please help.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76

5 Answers5

41

Another way:

convert *.jpg -resize 80% -set filename:f '%t' ../'%[filename:f].jpg'

Will place converted files in the folder above.

The option -set filename:f '%t' sets the property filename:f to the current filename without the extension. Properties beginning with filename: are a special case that can be referenced in the output filename. Here we set it to ../'%[filename:f].jpg, which ends up being the image filename with the extension replaced with .jpg in the parent directory.

Documentation references:

Flimm
  • 136,138
  • 45
  • 251
  • 267
Alistair Colling
  • 1,363
  • 2
  • 19
  • 29
  • For some reason, this doesn't work for me when the output is `.webp`. – Flimm Sep 21 '16 at 16:01
  • This should work indeed, but also in my case doesn't work (Windows 11, WebP); it instead just creates a single big file. – svenema Jun 28 '23 at 19:04
35

A simple solution would be copy, followed by mogrify - another imagemagick tool - this will keep the same names, it takes all the same args as convert.

cp *.jpg thumb/
cd thumb
mogrify -resize 120X120 *.JPG

Alternatively you could do a bit of shell scripting, using find -exec or xargs

# using -exec
find . -iname "*.JPG" -maxdepth 1 -exec convert -resize 120x120 {} thumbs/{} \;

# using xargs
find . -iname "*.JPG" -maxdepth 1 | xargs -i{} convert -resize 120x120 {} thumbs/{}
Adam
  • 35,919
  • 9
  • 100
  • 137
7

Another easy way that doesn't involve a lot of typing is GNU Parallel:

parallel convert {} -resize 120X120 thumb/{} ::: *.jpg

convert is called for each of the files given after :::, and {} is replaced with the file name for each invokation. This will also process the files in parallel, so it's likely a lot faster than the other solutions here.

It also works if you want to convert the file type:

parallel convert {} {.}.png ::: *.jpg

{.} is replaced with the filename without extension, allowing you to change it easily.

Luchs
  • 1,133
  • 1
  • 9
  • 10
2

Here's what I do:

Convert all files to filename-new.extension

for FILE in *; do convert -resize 320 $FILE $(echo $FILE | sed 's/\./-new./'); done

Move all filename-new.extension files back to filename.extension:

for FILE in *; do mv $FILE $(echo $FILE | sed 's/-new//'); done
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
0

In Windows, do this instead:

FOR /f "tokens=*" %G IN ('dir /b *.jpg') DO convert %G -resize 120x120 thumb\%G

Notes:

  • Make sure you create the thumb folder in advance.
  • Use %% intead of %, if you make this part of a batch (.bat) file.
  • A benefit over doing it all at once with one convert is that this is more memory efficient, probably..
svenema
  • 1,766
  • 2
  • 23
  • 45