Repeat image without going off canvas? Unix command line …

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
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Repeat image without going off canvas? Unix command line …

Post by mhulse »

Hello,

I'm having some issues figuring out how to tile an image, from the center, without allowing it to go outside of the image dimensions.

Here's my starting image:

Image

This works for tiling:

Code: Select all

$ convert -size 1100x650 tile:square.png tile.jpg
… which creates:

Image

But what I would like to create is this:

Image

Questions:

1. Is it possible to keep the tile from going outside of the canvas area?

2. How can I get the tile to start from the center? I've tried

Code: Select all

$ convert -gravity center -size 1100x650 tile:square.png tile.jpg
… but that didn't seem to do anything.


Many thanks in advance for your help!
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Repeat image without going off canvas? Unix command line …

Post by GeeMack »

mhulse wrote:Hello,

I'm having some issues figuring out how to tile an image, from the center, without allowing it to go outside of the image dimensions.
There are a few ways to approach this. Is it something you'll be doing for many files, or just a one-off? Are the tile sizes always the same? The canvas size? And what version of IM are you using?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Repeat image without going off canvas? Unix command line …

Post by fmw42 »

If you know your tile size, you can compute the canvas size you need in odd multiples of the tile size. Use that canvas size and it will always have a tile in the center and never go off the canvas.

try this (unix)

Code: Select all

canvaswd=1100
canvasht=650
tilewd=`convert -ping square.png -format "%w" info:`
tileht=`convert -ping square.png -format "%h" info:`
wratio=`convert xc: -format "%[fx:floor($canvaswd/$tilewd)]" info:`
hratio=`convert xc: -format "%[fx:floor($canvasht/$tileht)]" info:`
numwd=`convert xc: -format "%[fx:mod($wratio,2)==0?($wratio-1):$wratio]" info:`
numht=`convert xc: -format "%[fx:mod($hratio,2)==0?($hratio-1):$hratio]" info:`
wd=$((numwd*tilewd))
ht=$((numht*tileht))
convert -size ${wd}x${ht} tile:square.png tiled_image.jpg
mhulse
Posts: 61
Joined: 2016-03-17T18:46:22-07:00
Authentication code: 1151

Re: Repeat image without going off canvas? Unix command line …

Post by mhulse »

Hi GeeMack and fmw42, thanks so much for your quick replies and help, I really do appreciate it.
GeeMack wrote:There are a few ways to approach this. Is it something you'll be doing for many files, or just a one-off?
This will be for many files.

Basically, I'm starting with a 10,000px by 10,000px image and scaling down to multiple different canvas sizes. From there, I'd like to have the ability to tile the image across the canvas.

My thought was to have a script scale the image down to size and then tile the leftover area without getting cut off.

For example, 10,000x10,000 image proportionally scales down to a 2000px height canvas, and then repeats horizontally across canvas (from center) without the tiles going off the edges of the new canvas size.
GeeMack wrote:Are the tile sizes always the same? The canvas size? And what version of IM are you using?
The original image will be very large, but might not always be square.

The canvas size will vary as well.

Basically I want to fit the original image to the new size, proportionally, and then tile the image from the center if there's enough room to tile (either horizontally or vertically or both) the original scaled image.

I'm using:

Version: ImageMagick 6.9.3-0 Q16 x86_64 2016-02-19
fmw42 wrote:If you know your tile size, you can compute the canvas size you need in odd multiples of the tile size. Use that canvas size and it will always have a tile in the center and never go off the canvas.

try this (unix)
Ooooooh, that's awesome! It's almost exactly what I need!!! :)

This is very helpful. Looking at this code gives me a good idea how to get the exact result I am looking for.

I will post the final version back here if I end up figuring out how to modify to suit my exact needs.

Thanks fmw42, this example code is very helpful! :)

Thanks again GeeMack and fmw42, I really appreciate your help.

I'll be back with my code modifications.
Homebrew-installed ImageMagick 7.0.8-27 Q16 x86_64 2019-02-12 | macOS Mojave | MacBook Pro
Post Reply