Resize and combine

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
KKL
Posts: 3
Joined: 2019-02-25T02:49:03-07:00
Authentication code: 1152

Resize and combine

Post by KKL »

Hi!
I'd like to convert my images to squares with the same size.
I have a white (backgroud) square on which I'd like to place the actual image. It would be great if the background rectangle could be resized to match the MAX(width or height) of the actual picture.
Meta:
resize background-pic width and height to max (width(pic1),height(pic1)
place pic1 centered on resized background-pic
output to pic1_new
Is it possible with the command line tool?
Rgds
KKL
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize and combine

Post by snibgo »

What version of IM, on what platform? I'll assume IM v7 on Windows BAT.

Code: Select all

magick ^
  in.png ^
  -set option:MYSIZE %%[fx:max(w,h)]x%%[fx:max(w,h)] ^
  -size %%[MYSIZE] xc:Blue ^
  +swap ^
  -gravity Center ^
  -compose Over -composite ^
  out.png
I've used Blue because it's easier to see than White.
snibgo's IM pages: im.snibgo.com
KKL
Posts: 3
Joined: 2019-02-25T02:49:03-07:00
Authentication code: 1152

Re: Resize and combine

Post by KKL »

Hi!
Thank you for your reply.
I am using Win command line (dos window)
KKL
Posts: 3
Joined: 2019-02-25T02:49:03-07:00
Authentication code: 1152

Re: Resize and combine

Post by KKL »

WAU! It Works!!
Thak you so much!!!
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Resize and combine

Post by GeeMack »

KKL wrote: 2019-02-25T03:03:57-07:00resize background-pic width and height to max (width(pic1),height(pic1)
place pic1 centered on resized background-pic
output to pic1_new
Check snibgo's suggestion for a simple way to composite your input image over a square background. Here's a way to use "-extent" to expand the background to a "max(w,h)" square and produce the same result...

Code: Select all

magick input.png -gravity center -background white -extent %[fx:max(w,h)]x%[fx:max(w,h)] result.png
If you're using it in a BAT script you'll need to make those single percent signs "%" on the FX expressions into doubles "%%".
Post Reply