Large image tiling with specific filename format output

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
sedwo
Posts: 9
Joined: 2018-02-14T09:47:10-07:00
Authentication code: 1152

Large image tiling with specific filename format output

Post by sedwo »

I've tried the following with some success to output tiles of a large image:

Code: Select all

magick image1.png -crop 256x256 parts-%02d.png
outputs tiles at 256x256px:
parts-01.png
parts-02.png
parts-03.png
parts-04.png
But I still have a challenge. The filename format needs to output each tiles dimensional coordinate.
Example:
[column]
tile output filename: tile_0x0.png :
X position of the tile = 0
Y position of the tile = 0

tile output filename: tile_0x256.png :
X position of the tile = 0
Y position of the tile = 256

tile output filename: tile_0x512.png :
X position of the tile = 0
Y position of the tile = 512

[row]
tile output filename: tile_256x0.png :
X position of the tile = 256
Y position of the tile = 0

tile output filename: tile_512x0.png :
X position of the tile = 512
Y position of the tile = 0
Hence the tiles filename depict their coordinate on the tile map. As well as hint to their 2D dimension.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Large image tiling with specific filename format output

Post by fmw42 »

What is your IM version and platform? Please always provide that. I believe you will need to crop the images into tiles. Then read the tiles in a loop one at a time to extract the virtual canvas offsets and rename the files. You can use the -set filename:offsets "%Xx%Y" to get the offsets and then write those values to the output image. See

https://www.imagemagick.org/script/escape.php
https://www.imagemagick.org/Usage/basics/#set
sedwo
Posts: 9
Joined: 2018-02-14T09:47:10-07:00
Authentication code: 1152

Re: Large image tiling with specific filename format output

Post by sedwo »

Version: ImageMagick 7.0.7-22 Q16 x86_64 2018-01-22
On macOS, but will also be used on a Linux server.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Large image tiling with specific filename format output

Post by GeeMack »

sedwo wrote: 2018-02-14T10:13:18-07:00Hence the tiles filename depict their coordinate on the tile map. As well as hint to their 2D dimension.
You can crop an image into tiles and save the output images with filenames that indicate their dimensions and the offsets of their original locations. A command like this should get you started...

Code: Select all

magick image1.png -crop 256x256 -set filename:f "tile_%[w]x%[h]+%[fx:page.x]+%[fx:page.y]" "%[filename:f].png"
That would name the tiles something like "tile_256x256+768+256.png", with the cropped width and height, and the original X and Y offsets.

You could even include the original image dimensions with FX expressions like "%[fx:page.width]x%[fx:page.height]".

To use that in a Windows BAT script you'd need to change all the single percent signs "%" into doubles "%%".
sedwo
Posts: 9
Joined: 2018-02-14T09:47:10-07:00
Authentication code: 1152

Re: Large image tiling with specific filename format output

Post by sedwo »

You've been very helpful GeeMack,

I tested:

Code: Select all

magick kitten.png -crop 256x256 -set filename:f "tile_%[fx:page.x],%[fx:page.y]_%[w]x%[h]" "%[filename:f].png"
with results:
tile_0,0_256x256.png
tile_0,256_256x224.png
tile_256,0_256x256.png
tile_256,256_256x224.png
tile_512,0_128x256.png
tile_512,256_128x224.png
The format of
x,y_WxH
should work as well to be easily parsed.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Large image tiling with specific filename format output

Post by GeeMack »

sedwo wrote: 2018-02-14T10:13:18-07:00Hence the tiles filename depict their coordinate on the tile map. As well as hint to their 2D dimension.
If the tile images are saved as PNGs, they can still contain the original image size and offset information. ImageMagick can use that to reconstruct all or part of the original image. A simple command like this shows how it works...

Code: Select all

magick "tile_256x256+1024+256.png" -background none -flatten test.png
If you don't want the tiles to contain that paging information you can use "+repage" before the final output of the cropping command.
Post Reply