joining images of different dimensions with automatic resizing according to smallest

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
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

joining images of different dimensions with automatic resizing according to smallest

Post by manit »

I am using
$ display --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
on 64bit ubuntu OS
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial

Currently, I have two images of resolution 200x300 ('A') and 100x70 ('B') .
I am stacking them vertically using append operator.
But there is white space at the bottom of B in result.
It would be nice if we can specify A to resize to somethingx70 resolution maintaining the aspect ratio and then join with B.
Can this be done in a single command ?
Moreover, if we do not have information about image sizes then is there any operator that joins images vertically after resizing all images in a group to one with lowest y dimension ?

Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: joining images of different dimensions with automatic resizing according to smallest

Post by snibgo »

manit wrote:I am stacking them vertically using append operator.
But there is white space at the bottom of B in result.
At the right of B, surly?

Sample inputs and output would clarify what you want.

I don't think you can do this in a single command in v6. V7 can do the A and B problem in a single command, by using "-resize %[MYSIZE]" where MYSIZE is set with "fx:[]".

For the general problem, I would use a script to find the minimum dimension, then have one IM command to do the resizing and append.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: joining images of different dimensions with automatic resizing according to smallest

Post by GeeMack »

manit wrote: 2019-10-09T08:57:47-07:00Can this be done in a single command ?
Moreover, if we do not have information about image sizes then is there any operator that joins images vertically after resizing all images in a group to one with lowest y dimension ?
This can be done, but a command with IMv6 would be complicated and pretty messy. I worked up an example that works with ImageMagick 6.8.9-9 on bash and ImageMagick 6.9.10-11 on Windows 10. This starts with four of IM's built-in images of various sizes, scales them all to match the height of the shortest one, then appends them horizontally. It should work with any reasonable number of images, and the shortest image can be anywhere in the list.

Code: Select all

convert logo: wizard: rose: granite: \
   -duplicate 1,0 -set page "+%[fx:s.w]+%[fx:s.h]" \
   -set page "+%[fx:s.w]+%[fx:min(u[max(0,t-1)].page.y,s.h)]" \
   -delete 0 -set page "+%[fx:s.h]+%[fx:u[-2].page.y]" \
   -insert 0 +distort SRT "%[fx:s.page.y/s.h] 0" \
   -shave 1 +append +repage out.png
It compares each image to the previous, and carries the smaller height along through the series using "-set" and FX expressions to manipulate variables stored in each image's paging geometry offsets. It requires some tricky use of a dummy duplicate image, setting and resetting the "variables", and some clumsy rearranging of the order of the list.

IMv6 has limited use of FX expressions, so the command uses "+distort SRT" to scale all the images to that smallest height. Then it shaves off that extra row of pixels added by "+distort" and does an "+append" to create the output.

The command works exactly the same from a Windows command line by simply changing the continued line backslashes "\" to carets "^".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: joining images of different dimensions with automatic resizing according to smallest

Post by snibgo »

Ingenious.
snibgo's IM pages: im.snibgo.com
Post Reply