Page 1 of 1
Preserving input filename in batch convert
Posted: 2007-01-09T22:37:43-07:00
by taob
I have a script that takes small images (450x300) and creates a square thumbnail for each one via a resize and a crop. Traditionally, I've been using a for-loop in a /bin/sh script:
Code: Select all
$ time for i in *.jpg ; do convert -gravity northwest $i -resize 45x45 -crop 30x30+0+0 thumbs/$i ; done
real 2m26.706s
user 1m39.739s
sys 1m16.515s
This is ImageMagick 6.3.0 (11/05/06 Q16) under Cygwin under Windows XP, and I would like to improve the performance and efficiency of this batch process. After reading Anthony's excellent notes at
http://www.cit.gu.edu.au/~anthony/graphics/imagick6/, I discovered I could do something like this:
Code: Select all
$ time convert -gravity northwest '*.jpg[45x45]' -crop 30x30+0+0 thumbs/
real 1m7.884s
user 1m0.451s
sys 0m6.577s
In both examples, a total of 1222 JPEGs were processed. By avoiding the 1221 extraneous forks, I can cut the running time by more than half. The problem is that the output filenames are not in the format I need. I do not need %d, %o or %x... I simply want the output filename to be the same as the input filename, and I cannot find a way to do that. IM appears to insist on using a counter variable! Is there a way to disable this, and simply preserve the filename as-is?
The output JPEGs are exactly as I like them... it's ironic that a file naming issue is the only remaining obstacle. So close, yet so far away...
Posted: 2007-01-10T10:24:37-07:00
by magick
If you grab ImageMagick 6.3.1-7 Beta, available tommorrow, you can use this command to accomplish a lightweight resize/crop:
- mogrify -path thumbs -gravity northwest -resize 45x45 -crop 30x30+0+0 *.jpg
Posted: 2007-01-10T10:39:19-07:00
by taob
Ah, so the new "-path" option specifies the output directory then? Thanks, I'll check out the new beta once it is available!
I don't know if this is on the "todo" list, but it would be great to have more format specifiers for output filenames too, e.g.: %f for the input filename (without extension), %e for input filename extension, etc. Of course, once you go there, you might as well give us the ability to extract EXIF and IPTC metadata info and use that in filenames too.
Posted: 2007-01-11T22:07:11-07:00
by anthony
You may be able to speed up things further with large JPEGs by adding a -size argument to ask the library to reduce the amount returned from the file.
See IM Examples, Thumbnailing.
PS: Cristy -path is a nice addition. I made a note to comment on it in image saves and thumbnail generation.
Posted: 2007-01-11T22:32:37-07:00
by taob
anthony wrote:
You may be able to speed up things further with large JPEGs by adding a -size argument to ask the library to reduce the amount returned from the file.
Hi, Anthony. I use "-size" when resizing down large 12- and 16-megapixel JPEGs, but it does not seem to make much difference with 400x600 source images. Well, that was tested using a shell for-loop, and I'm sure the fork overhead would have buried any performance gain from -size.
I haven't had time to play with 6.3.1-7 yet, but it looks like -path will do the trick. Thanks!
Re: Preserving input filename in batch convert
Posted: 2008-10-09T17:42:31-07:00
by anthony
You can not modify the name in this way, basically as the output format does not have a option to specify more than 'scene' number. This may change in the future.
The only way to do that is to either mogrify to a different directory or format, then use a program like "mved", "mv_perl", "mvsed, or one of the other many 'mv multiple with filename edit' type scripts that people have written and published.
Of course you can go back to the original 'looped' script version (running lots of separate commands). Or write a API script (in perl, python, ruby, etc) that will read/modify/write the images in a loop in the one process.
For examples of this see IM Exmaples, Basics, Mogrify, alternatives..
http://www.imagemagick.org/Usage/basics/#mogrify_not