Score:5

ImageMagick convert on a multiple files

ru flag

I have 3 files in a directory:

aaa.jpg    
bbb.jpg  
ccc.jpg  

I can scale down an image using ImagkMagick convert:

convert aaa.jpg -resize 1200x900 aaa-small.jpg  

I want to do all the images in the directory, something like:

convert *.jpg -resize 1200x900 *-small.jpg  

but this results in files named like so:

*-small-0.jpg  
*-small-1.jpg  
*-small-2.jpg  

What I want is:

aaa-small.jpg  
bbb-small.jpg  
ccc-small.jpg  

How do I do this?

Brian Z avatar
uz flag
Mogrify is the ImageMagick command designed for batch jobs. Question is a related to and possibly a duplicate of this one: https://superuser.com/questions/597428/how-can-i-run-mogrify-but-prefix-the-filename
Score:9
hr flag

It's frustratingly opaque in the documentation, but you can pass a quoted shell glob to convert (quoted to prevent the shell from expanding it prematurely), and use Filename Percent Escapes to construct output filenames in the form %[filename:label] (where label is an arbitrary user-specified label), using the input basename escape %[basename] or its legacy equivalent %t:

$ ls ???.jpg
aaa.jpg  bbb.jpg  ccc.jpg

then

$ convert '*.jpg' -set filename:fn '%[basename]-small' -resize 1200x900 '%[filename:fn].jpg'

resulting in

$ ls ???-small.jpg
aaa-small.jpg  bbb-small.jpg  ccc-small.jpg
Sampo Smolander avatar
cn flag
This approach loads all files into memory at the same time, so if you try to process tens or hundreds of files (not just 3), you might hit ImageMagick's "cache resources exhausted" error. You could edit `/etc/ImageMagick-6/policy.xml` to allow for more memory, but a shell loop might be easier.
Score:6
jp flag

In a for loop it is possible to use the features described in man bash at

Parameter Expansion
...
  ${parameter%%word}
      Remove matching suffix pattern.  The word is expanded to produce a pattern just
      as in pathname expansion.  If the pattern matches  a  trailing portion  of the
      expanded  value of parameter, then the result of the expansion is the expanded
      value of parameter with the shortest matching pattern (the ``%'' case) or the
      longest matching pattern (the ``%%'' case) deleted.  If parameter is @ or *,
      the pattern removal operation is applied  to  each positional parameter in turn,
      and the expansion is the resultant list.  If parameter is an array variable
      subscripted with @ or *, the pattern removal operation is applied to each member
      of the array in turn, and the expansion is the resultant list.

The following one-liner should do the job

for f in ./*.jpg ; do convert "$f" -resize 1200x900 "${f%.jpg}-small.jpg" ; done

This works in bash, which is the standard shell of Ubuntu. I think it is easier to remember than the elegant method by Steeldriver (who uses only convert and no for construct).

rexkogitans avatar
cn flag
+1 because it follows the Unix philosophy that each tool should do small part of the work efficiently, rather than one tool does everything.
7efkvNEq avatar
gh flag
I suggest changing the filenames from `*.jpg` to `./*.jpg` in case some name begins with `-`.
sudodus avatar
jp flag
@7efkvNEq, Thanks, that's a good idea.
Score:-2
bd flag
mkdir small
for f in *.jpg ; do convert $f -resize 1200x900 small/$f ; done
ru flag
This does not answer the question. The filenames are the same.
sudodus avatar
jp flag
@AlLelopath, True, the filenames are the same, but in another directory. I have used this method and I think it is good ;-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.