Adding a margin of repeated pixels around edges of image

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
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Adding a margin of repeated pixels around edges of image

Post by owntheweb »

Hello ImageMagick gurus,
I'm preparing textures for a 3D model and need to add a margin of repeated pixels around the edges. This will prevent odd seams at the edges of my model UV maps, adding a bit of margin there as well.

The image is 4054 pixels wide/high and want to resize to 4096, with the edges filled in with the repeating pixels.

I think I found the key at:
http://www.imagemagick.org/discourse-se ... hp?t=34525

I'm just not quite sure how to apply that to my use case just yet. I'm guessing this would happen with a negative crop to add borders then a variation from the link above?

Thanks for taking a look in advance!
Twitter | Blog | Worlds to explore. Worlds to create.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Adding a margin of repeated pixels around edges of image

Post by GeeMack »

owntheweb wrote: 2019-03-22T11:46:24-07:00The image is 4054 pixels wide/high and want to resize to 4096, with the edges filled in with the repeating pixels.

I think I found the key at:
http://www.imagemagick.org/discourse-se ... hp?t=34525

I'm just not quite sure how to apply that to my use case just yet. I'm guessing this would happen with a negative crop to add borders then a variation from the link above?

It's always more helpful if you provide the version of IM you're using and what platform or OS you're working on.

Assuming v7 since that's used in the linked example, a command like this should put any size image in the center of a 4096x4096 canvas, and fill any difference in dimensions by repeating the edge rows of pixels from the input image.

Code: Select all

magick input.png -set option:distort:viewport 4096x4096 ^
   -virtual-pixel edge -distort SRT "%[fx:w/2],%[fx:h/2] 1 0 2048,2048" output.png
That command is in Windows syntax. To use on a *nix shell you'll have to at least change the continued line caret "^" to a backslash "\".
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Adding a margin of repeated pixels around edges of image

Post by owntheweb »

Hello GeeMack,
Thanks for your response! I actually just read another response asking for version and should have posted.

I'm using:
ImageMagick 6.9.7-4 Q16 x86_64 20170114

I'll give this a shot as early as tonight and will report. Thanks again!!
Twitter | Blog | Worlds to explore. Worlds to create.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding a margin of repeated pixels around edges of image

Post by fmw42 »

Since you are using IM 6, you will have to change "magick" to "convert" in GeeMack's solution.
User avatar
owntheweb
Posts: 20
Joined: 2011-05-24T06:30:15-07:00
Authentication code: 8675308
Location: Colorado

Re: Adding a margin of repeated pixels around edges of image

Post by owntheweb »

This worked wonders. Thank you!!! :D :D :D
Twitter | Blog | Worlds to explore. Worlds to create.
Post Reply