Odd string formatting results for output file names...

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Odd string formatting results for output file names...

Post by GeeMack »

Using IM 7.0.5-0 on Windows 10 64.

I start by making an animated GIF just to use as an example of a multi-layer image with this command...

Code: Select all

magick rose: -duplicate 5 -virtual-pixel tile ^
   -distort affine "0,0 0,%[fx:t*h/n]" -set delay 50 -loop 0 animrose.gif
Then I convert that "animrose.gif" into PNG files from the separate layers like this...

Code: Select all

magick animrose.gif -set filename:f "%[t]_" %[filename:f]%02d.png"
This creates the six images I'd expect, but the file names have done something funny...

Code: Select all

animrose_%02d00.png
animrose_%02d01.png
animrose_%02d02.png
animrose_%02d03.png
animrose_%02d04.png
animrose_%02d05.png
The files get numbered and padded to two places with leading zeroes, but the string formatting code "%02d" is still in there as part of the file name.

If I run the same command but put that underscore in the output file name at the end instead of in the "-set filename:f" like this...

Code: Select all

magick animrose.gif -set filename:f "%[t]" %[filename:f]_%02d.png"
I get an even stranger result...

Code: Select all

animrose_%02d.00.png
animrose_%02d.01.png
animrose_%02d.02.png
animrose_%02d.03.png
animrose_%02d.04.png
animrose_%02d.05.png
The filenames obtain an extra dot "." before the sequence numbers.

These discrepancies seem to behave differently when using different input and/or output file formats, too. If I start with this multi-layer TIF...

Code: Select all

magick rose: -duplicate 5 -virtual-pixel tile -distort affine "0,0 0,%[fx:t*h/n]" multirose.tif
... and run essentially the same command as above that inserted the extra dots "." in the filenames, those dots don't appear...

Code: Select all

multirose_%02d00.png
multirose_%02d01.png
multirose_%02d02.png
multirose_%02d03.png
multirose_%02d04.png
multirose_%02d05.png
And if I make the rose into a multipage PDF...

Code: Select all

magick rose: -duplicate 5 -virtual-pixel tile -distort affine "0,0 0,%[fx:t*h/n]" pdfrose.pdf
... and run it through this command...

Code: Select all

magick pdfrose.pdf -set filename:f "%[t]" %[filename:f]_%02d.jpg"
... it actually inserts an extra "j" and the extra dot "." in each filename. So that's weird.

Code: Select all

pdfrose_%02d.j00.jpg
pdfrose_%02d.j01.jpg
pdfrose_%02d.j02.jpg
pdfrose_%02d.j03.jpg
pdfrose_%02d.j04.jpg
pdfrose_%02d.j05.jpg
Now it could be using this sort of string formatting for output file names is undefined behavior when mixing it with "%[filename:f]", and maybe I shouldn't expect it to work at all. :) But if any of the combinations above are supposed to produce sequentially numbered output file names according to the string formatting "%02d", something isn't quite right. Any ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Odd string formatting results for output file names...

Post by fmw42 »

You have only 3 quotes rather than 4. However, that does not seem to make a difference for me.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Odd string formatting results for output file names...

Post by snibgo »

I assume you ran these at the command line, not in a BAT file?
GeeMack wrote:... something isn't quite right. Any ideas?"
Agreed, testing with v6.9.5-3 and v7.0.3-5.

A workaround is to do all the string processing in the "-set filename", eg:

Code: Select all

convert animrose.gif -set filename:f "%[t]_%[s]" %[filename:f].png
But we can't then get leading zeroes.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Odd string formatting results for output file names...

Post by GeeMack »

snibgo wrote: 2017-02-21T11:07:43-07:00I assume you ran these at the command line, not in a BAT file?
That is correct, yes.
But we can't then get leading zeroes.
Ay, there's the rub.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Odd string formatting results for output file names...

Post by GeeMack »

fmw42 wrote: 2017-02-21T11:04:57-07:00You have only 3 quotes rather than 4. However, that does not seem to make a difference for me.
Yep. That'd be a mistake typing the command in my haste. I can't recall the exact combination I used, but one attempt actually generated filenames with two extension, like "outfile_%02d00.jpg.jpg". Unexpected results in any case.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Odd string formatting results for output file names...

Post by GeeMack »

A couple more tests of input and output file names. I copied the PDF above to a new file named "pdf_rose_6pages.pdf". This command...

Code: Select all

magick pdf_rose_6pages.pdf -set filename:f "%[t]_" "%[filename:f]%02d.jpg"
... creates these files with part of the original name truncated...

Code: Select all

pdf_rose_6pag00.jpg
pdf_rose_6pag01.jpg
pdf_rose_6pag02.jpg
pdf_rose_6pag03.jpg
pdf_rose_6pag04.jpg
pdf_rose_6pag05.jpg
Copying the original 6 page PDF to a file named "p.pdf" and running this command...

Code: Select all

magick p.pdf -set filename:f "%[t]" "%[filename:f]_%02d.jpg"
... results in a single output file named "p_%02d.jpg.jpg". Interesting. It may be more related to the length of the file names than with the image format of the input/output files.
Post Reply