Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick command to convert and save with same name

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.

like image 877
Avisek Chakraborty Avatar asked Apr 03 '12 11:04

Avisek Chakraborty


People also ask

What is XC in ImageMagick?

Yes it is purely an alias for xc: which stood for "X window color", which came from the extreme early days of ImageMagick (long before my time). Of course color is usually a SVN color rather than X window color, so really the original name is rather useless.

Does ImageMagick have GUI?

Left-clicking on an image brings up a simple, standalone menu (the only GUI feature you'll see in ImageMagick).


2 Answers

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:

  • -set documentation, which mentions the filename: special case
  • %t and other Format and Print Image Properties
like image 127
Alistair Colling Avatar answered Oct 20 '22 08:10

Alistair Colling


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/{} 
like image 41
Adam Avatar answered Oct 20 '22 07:10

Adam