concat/montage and resize images

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
whatdoineed2do
Posts: 1
Joined: 2011-06-04T02:51:23-07:00
Authentication code: 8675308

concat/montage and resize images

Post by whatdoineed2do »

I want to concatenate a few images (photos) which are different sizes, lets say image A 300x200 and image B 150x350

I want the final concatenated image to be the height of the smallest image but aspect preserved for both images.

So in the example, img A would be resized in our example to 150 (height of img B). I want the IM utils to determine the scaling as I want IM to figure out the max height.

Is this possible and if so how would one do this with montage/convert?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: concat/montage and resize images

Post by fmw42 »

You would have to write a short script or set of command lines to get each image's dimensions, put them into variables, then use those variables to compute the sizes you want for each image as new variables, then write a convert command to apply -resize to each as appropriate using the new variables and then append the two images side-by-side or vertically as desired.

IM does not have any means to my knowledge to do what you want automatically at the current time.

The set of command lines to do the computations will be different if you are on Unix or Windows. For windows users see http://www.imagemagick.org/Usage/windows/

You don't say what version of IM you are using or what platform, so it is hard to give you any examples.

In unix, you can do this as

image1:
Image

image2:
Image

image1="zelda3.jpg"
image2="cyclops.jpg"
h1=`convert $image1 -ping -format "%w" info:`
h2=`convert $image2 -ping -format "%w" info:`
min=`convert xc: -format "%[fx:min($h1,$h2)]" info:`
convert $image1 $image2 -resize ${min}x${min} +append zelda_cyclops.jpg

Image

see string formats and fx calculations at:
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/script/fx.php
http://www.imagemagick.org/Usage/transform/#fx
http://www.imagemagick.org/Usage/transform/#fx_escapes
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: concat/montage and resize images

Post by anthony »

You basically want to resize all images to have the same height which is equal to the smallest hieght of all the input images. Is that correct?


At this time I can only see this being done as a two command script. In the future it may be posible to do it in one command, but not at this time.

Finding the minimum height is just a task of scanning the outout of identify on the images. You can use
identify -ping images...
for this

The second part is just a resize operation, just before you append.
convert images... -resize x{height} +append result.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
MHC
Posts: 1
Joined: 2013-01-05T06:03:54-07:00
Authentication code: 6789

Re: concat/montage and resize images

Post by MHC »

For anyone searching for a premade script for this, here's one I compiled. This is more of a rough cut than a perfected script. I'm sure it's way more complicated than necessary. What matters, though is that it gets the job done (for a maximum of three images, that is).

Code: Select all

#!/bin/bash

# Output directory and file names
TIME=$(date +"%Y-%m-%d_%H%M%S")
WORKINGDIR=$(dirname "$1")

FILE1=$(basename "$1")
FILE2=$(basename "$2")
FILE3=$(basename "$3")

# Output file type
MIME1=$(file -ib "$1")
MIME2=$(file -ib "$2")
MIME3=$(file -ib "$3")

if [ "$MIME1" = "image/png; charset=binary" -o "$MIME2" = "image/png; charset=binary" -o "$MIME3" = "image/png; charset=binary" ]
  then
      TYPE=png
  else
      TYPE=jpg
fi

# Input dimensions
HEIGHT1=$(identify -format "%h" "$1")
HEIGHT2=$(identify -format "%h" "$2")

if [ $# -gt 2 ]
  then
      HEIGHT3=$(identify -format "%h" "$3")
      XDIM=$(echo -e "$HEIGHT1\n$HEIGHT2\n$HEIGHT3" | sort -rn | tail -1)
      TXT="\n 1 = $FILE3\n 2 = $FILE2\n 3 = $FILE1"

  else
      XDIM=$(echo -e "$HEIGHT1\n$HEIGHT2" | sort -rn | tail -1)
      TXT="\n 1 = $FILE2\n 2 = $FILE1"
fi

# Choose orientation
INPUT=`zenity --width 200 --height 100 --entry --title "Intellimerge" --text="\`printf "How do you want the images to be ordered?$TXT"\`"`

if [ "$INPUT" = "12" ]
  then
      ORDER="$2 $1"

elif [ "$INPUT" = "21" ]
  then
      ORDER="$1 $2"

elif [ "$INPUT" = "123" ]
  then
      ORDER="$3 $2 $1"

elif [ "$INPUT" = "132" ]
  then
      ORDER="$3 $1 $2"

elif [ "$INPUT" = "213" ]
  then
      ORDER="$2 $3 $1"

elif [ "$INPUT" = "231" ]
  then
      ORDER="$2 $1 $3"

elif [ "$INPUT" = "312" ]
  then
      ORDER="$1 $3 $2"

elif [ "$INPUT" = "321" ]
  then
      ORDER="$1 $2 $3"
fi

# Merge files
convert $ORDER -resize x$XDIM +append "$WORKINGDIR/Merged_$TIME.$TYPE"
Feel free to improve upon it. I would appreciate it if you shared your improvements with me.
Post Reply