0-padding numbers in filename when using fx calculation

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
occular
Posts: 2
Joined: 2016-12-21T04:00:58-07:00
Authentication code: 1151

0-padding numbers in filename when using fx calculation

Post by occular »

I'm splitting a large image into tiles, with the X, Y coord of each tile in the tile filenames:

Code: Select all

convert "livingroom0-2560x1280.jpg" -crop 512x512 -set filename:tile "%[fx:page.x/512]_%[fx:page.y/512]" \
    +repage "tile_%[filename:tile].png"
This works fine but I'd like to 0-pad the filenames so that instead of "tile_3_1.png" I get "tile-03_01.png".
Can anyone give me a hint how to do this?

My best effort so far is:

Code: Select all

convert "livingroom0-2560x1280.jpg" -crop 512x512 -set filename:yLoc "%[fx:page.y/512]" \
     -set filename:xLoc "%[fx:page.x/512]" +repage "tile_%[filename:xLoc]_%[filename:yLoc].png"
But this doesn't work, I get filenames like"tile_3_%[f1.png".
occular
Posts: 2
Joined: 2016-12-21T04:00:58-07:00
Authentication code: 1151

Re: 0-padding numbers in filename when using fx calculation

Post by occular »

Sorry, can't edit my post for some reason, the second code example should read:

convert "livingroom0-2560x1280.jpg" -crop 512x512 -set filename:yLoc "%[fx:page.y/512]" \
-set filename:xLoc "%[fx:page.x/512]" +repage "tile_%02[filename:xLoc]_%02[filename:yLoc].png"

and when I run that, I get errors.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 0-padding numbers in filename when using fx calculation

Post by anthony »

At the moment there is no way to 'format' the number output from a FX calcualtion. It was thought about an planned though.
see the Plan.... in Future Developments section of IM Examples
http://www.imagemagick.org/Usage/bugs/
IMv7 Percent Escapes and FX Expression Development
Section "Formatting Percent Escapes..."
http://www.imagemagick.org/Usage/bugs/I ... rcent.html
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply