Output file globbing

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
jwes
Posts: 3
Joined: 2017-08-29T09:04:12-07:00
Authentication code: 1151

Output file globbing

Post by jwes » 2017-09-03T11:08:29-07:00

I have a program that outputs a number of files, e.g. in1,in2,...in499,in500. I want to use IM to do the same thing to each of these files.
in1 --> out1
in2 --> out2
...
in500 -->out500

On windows 7, using IM 7.0.6, if I do magick in*.png -dosomething out%d.png, they come out in some odd order. I can do magick in%d.png[1-500] -dosomething out%d.png, which gives in1 --> out0, ..., as long as the input files are all in sequence. What I would like to do is magick in*.png -dosomething out%s.png, which could replace the '%s' with whatever is matched by the '*'. Any suggestions?

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Output file globbing

Post by GeeMack » 2017-09-03T12:06:07-07:00

jwes wrote:
2017-09-03T11:08:29-07:00
On windows 7, using IM 7.0.6, if I do magick in*.png -dosomething out%d.png, they come out in some odd order. I can do magick in%d.png[1-500] -dosomething out%d.png, which gives in1 --> out0, ..., as long as the input files are all in sequence. What I would like to do is magick in*.png -dosomething out%s.png, which could replace the '%s' with whatever is matched by the '*'. Any suggestions?
There are a couple ways to approach this. The simplest method for specifying the order of the named output files is to modify your string formatting of the output file name something like this...

Code: Select all

magick input*.png -resize 50% output%03d.png
Using "%03d" there will force IM to pad the numbers with zeros to three places. The output will be "output000.png", "output001.png" "output002.png", etc. You can tell it to pad with as many zeros as needed to accommodate the expected number of output files with "%02d" for two places, or "%09d" for nine places, or whatever. Also, if you want the output files to start with a number other than zero, use "-scene N" before the output where "N" is the starting number for your sequence.

Another angle would be to use the input file names to create the output file names. You can do something like this...

Code: Select all

magick input*.png -set filename: "out_%[t]" -resize 50% "%[filename:].png"
That will substitute the "%[t]" for the input file name without the extension, and add the "out_" as a prefix. Your output files will be named something like "out_inputname1.png", "out_inputname2.png", "out_inputname3.png", etc.

Find out more about using IM's formatting escapes at THIS link.

In a BAT script you'll have to make all those single percent signs "%" into doubles "%%".

snibgo
Posts: 9399
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Output file globbing

Post by snibgo » 2017-09-03T13:32:14-07:00

Another possibility is to have one convert per input file, in a shell script loop that processes all in*.png files, and creates an output name something like:

Code: Select all

set outfile=%infile:in=out%
Then if an infile is named in543.png, the outfile would be named out543.png.
snibgo's IM pages: im.snibgo.com

Post Reply