ImageMagick command to convert and save with same name
Asked Answered
C

5

35

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.

Corie answered 3/4, 2012 at 11:18 Comment(0)
H
38

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/{}
Hangout answered 3/4, 2012 at 11:20 Comment(1)
thanx man. it worked. Will check out the exec & xargs also. thanks again.Corie
F
44

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:

Fritzie answered 29/1, 2015 at 18:19 Comment(2)
For some reason, this doesn't work for me when the output is .webp.Mirella
This should work indeed, but also in my case doesn't work (Windows 11, WebP); it instead just creates a single big file.Differential
H
38

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/{}
Hangout answered 3/4, 2012 at 11:20 Comment(1)
thanx man. it worked. Will check out the exec & xargs also. thanks again.Corie
E
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.

Eng answered 27/9, 2015 at 17:7 Comment(0)
T
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
Teetotal answered 3/7, 2014 at 12:20 Comment(0)
D
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..
Differential answered 28/6, 2023 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.