2
votes

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.

3

3 Answers

3
votes

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.

3
votes

I don't know why you would need to overlay the 8 tiles on the original. Just create it from scratch and name the output the same as your original

You could use Imagemagick montage to do that (unix syntax):

nx=`convert original.png -format "%[fx:w/32]" info:`
ny=`convert original.png -format "%[fx:h/32]" info:`
num=$((nx*ny-1))
montage tile.png -duplicate $num -tile ${nx}x${ny} -geometry +0+0 result.png


enter image description here

Here I use convert to duplicated the tile, but it uses a relatively current -duplicate feature. If you do not have a current enough version of Imagemagick, then just repeat the tile in montage as follows:

montage Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png Ro1Lp.png -tile 2x8 -geometry +0+0 result.png


0
votes

As Fred (fmw42) says, "why don't you just create the whole image from scratch?".

Maybe your description isn't complete, so here are a couple more pieces that might help you work it out.

Given bluetiles.png and singlered.png:

enter image description here enter image description here

you can position red ones as you wish like this:

convert bluetiles.png \
   singlered.png -geometry +0+32  -composite \
   singlered.png -geometry +32+96 -composite result.png

enter image description here


Given bluewithtransparent.png:

enter image description here

you can copy its transparency to the newly-created image like this:

convert bluetiles.png \
   singlered.png -geometry +0+32  -composite \
   singlered.png -geometry +32+96 -composite \
\( bluewithtransparent.png -alpha extract \) -compose copyopacity -composite result.png

enter image description here