The different styles of auto naming the tiles (-crop %02d)

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
forumim

The different styles of auto naming the tiles (-crop %02d)

Post by forumim »

I have a big image and crop it in little tiles:

Code: Select all

exec("convert input.jpg -crop 256x256 mytile_%02d.jpg", $arr, $err);
Now the generated tiles are named:
mytile_01.jpg, mytile_02.jpg, mytile_03.jpg ...

I would like, that the tiles in the first line are named:
mytile_01-01.jpg, mytile_01-02.jpg, mytile_01-03.jpg ...

Tiles in the second line:
mytile_02-01.jpg, mytile_02-02.jpg, mytile_02-03.jpg ...

Third line:
mytile_03-01.jpg, mytile_03-02.jpg, mytile_03-03.jpg ...

And so on...

How can I do that?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: The different styles of auto naming the tiles (-crop %02d)

Post by anthony »

That is not so easy, as the output filename only allows the use of
a single %d printf() format to specify the image size.

What you may need to do is either...
  • rename the files afterward converting the sequence
    01 02 03 04 05....
    into
    01-01 01-02 ... 01-30 02-01 02-02 ...
    etc. You will need to know how many tiles per row was generated though.
  • JPEG does not save the 'offset' from which an image is cropped. But PNG format does (it is also not color lossy). You can output the images as PNG, then read the virtual canvas offset, and from that determine the 'tile' position, allowing you to rename the image, or resave as JPEG images of the right name.
  • You can record the images virtual canvas offset
    (position from where the image was cropped) into the images JPEG comment string before you remove that offset from the resulting image (JPEG does not store the virtual canavs information, so it losses that information)

    Code: Select all

    convert input.jpg -crop 256x256 -set comment '%O' +repage mytile_%02d.jpg"
    identify -format '%f %c' mytile_%02d.jpg
  • In a FUTURE version of IM, you may be able to specify a filename for the output file (perhaps as a %s string). This might be able to also be able to be -set and the tile number calculated using %[FX:...] escapes.

    However at this time the filename is not adjustable using -set, AND the X and Y virtual offsets of the image is not available within FX expressions. I have put in a request for the later, so the tile position can be at least calculated and recorded in comments. You may like to request the former '%s' to set the save images basename
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
forumim

Re: The different styles of auto naming the tiles (-crop %02d)

Post by forumim »

Thanks a lot!

A such skill in a FUTURE version of IM would be great.

So I will looking forward to the next versions of IM,
till then I rename the files afterward.
(I know how many tiles per row was generated)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: The different styles of auto naming the tiles (-crop %02d)

Post by Bonzo »

Another way would be to crop the image into rows and then crop the rows into tiles; you could then name each row whatever you wanted.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: The different styles of auto naming the tiles (-crop %02d)

Post by Bonzo »

I am slow sometimes; if you are using php you could modify this code to rename the images:
http://www.rubblewebs.co.uk./imagemagic ... _f_42.html
Post Reply