imagemagickoverlayplaceholdertiletiling

ImageMagick: Replace All Image Tiles with Single Tile


I'm assuming ImageMagick is the best option for this, but please let me know if you have other recommendations that can be scripted.

I am trying to replace all the 32x32 tiles of an image with a single tile. This is an example for the original image:

Original

This is the tile that I want to use to replace all tiles on the original image:

Placeholder tile

And this is what I want the output to be:

Placeholder image

I've figured out from other posts on Stack Overflow that I can use ImageMagick's composite option to overlay the tile onto the original image:

$ convert original.png tile.png -composite overlay.png

Resulting in the following:

Overlay

And I assume by knowing the original images dimensions I can overlay the tile manually multiple times. But is there a way to automate the process. In the example pictures I have given, I need to overlay the tile 8 times on the original 64x128 image.

How can I do this with ImageMagick or another software? And if ImageMagick, would the montage or composite command be a better option?

Edit: As an additional question, would it be possible to skip tiles that are completely transparent?

Input example:

Original transparent

Output example:

Placeholder transparent

It isn't really important to be able to do this part, but would be nice.


Solution

  • If the tile image fits evenly into the dimensions of the original, a command like this should do most of what you want...

    convert original.png tile.png -background none -virtual-pixel tile \
       -set option:distort:viewport %[fx:u.w]x%[fx:u.h] -distort SRT 0 +swap \
       -compose copyopacity -composite overlay.png
    

    That reads in both images. Then it creates another canvas the size of the original and filled with multiple copies of the tile image. Then it uses the original as a transparency mask to create a copy of the new tiled image with the same transparent cells as the original.