convert & resize & crop & composite : unexpected results

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
32d2d7ee
Posts: 11
Joined: 2016-09-19T01:00:08-07:00
Authentication code: 1151

convert & resize & crop & composite : unexpected results

Post by 32d2d7ee »

I have made a compositie of three images. This works fine!

Image
Command used:

Code: Select all

magick convert -units PixelsPerInch -size 2362x3543 xc:white 
( ADH_20170808_5539.jpg -auto-orient -resize 2362x )  -geometry +0+0 -composite 
( ADH_20170808_5578.jpg -auto-orient -resize 1175x )  -geometry +0+1777 -composite 
( ADH_20170808_5619.jpg -auto-orient -resize 1175x )  -geometry +1187+1777 -composite  -density 300 Sample_no_crop.jpg
Due to the camera aspect ratio the size of the image becomes: 2362x1575.
However my specs say that the top image should have the size 2362x1766, so I have to resize and crop.
On a single image I used this command and it works perfect. I get the right results.

Code: Select all

magick convert ADH_20170808_5539.jpg -auto-orient -resize "2362x1766^" -gravity Center -crop 2362x1766+0+0 +repage crop_image.jpg
Then I integrated this command in the first command:

Code: Select all

magick convert -units PixelsPerInch -size 2362x3543 xc:white 
( ADH_20170808_5539.jpg -auto-orient -resize "2362x1766^" -gravity Center -crop 2362x1766+0+0 +repage  )  -geometry +0+0 -composite 
( ADH_20170808_5578.jpg -auto-orient -resize "1175x1766^" -gravity Center -crop 1175x1766+0+0 +repage  )  -geometry +0+1777 -composite 
( ADH_20170808_5619.jpg -auto-orient -resize "1175x1766^" -gravity Center -crop 1175x1766+0+0 +repage  )  -geometry +1187+1777 -composite  -density 300 Sample_crop.jpg
This results in:
Image
Issue: the image is not generated as expected?

Question: What am I doing wrong?
What would be the right command?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert & resize & crop & composite : unexpected results

Post by snibgo »

"-composite" respects the gravity setting. See http://www.imagemagick.org/script/comma ... s.php#crop

Your first command had no gravity setting, so it is undefined, which is similar (but not identical) to "-gravity NorthWest".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert & resize & crop & composite : unexpected results

Post by fmw42 »

User snibgo has a good point. I would add that some settings inside parentheses "leak" outside and so your composite commands are probably getting -gravity center. If that is the case, you can fix that by adding -gravity northwest before -geometry on each command. Or perhaps by adding -respect-parenthesis -gravity northwest right after convert.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert & resize & crop & composite : unexpected results

Post by snibgo »

I would do it like this (with whatever your shell needs for line continuation and escapes). In the first command, insert "-gravity NorthWest". Does it do what you want?

Code: Select all

magick convert 
-units PixelsPerInch -size 2362x3543 xc:white 
-gravity NorthWest
( ADH_20170808_5539.jpg -auto-orient -resize 2362x ) -geometry +0+0 -composite 
( ADH_20170808_5578.jpg -auto-orient -resize 1175x ) -geometry +0+1777 -composite 
( ADH_20170808_5619.jpg -auto-orient -resize 1175x ) -geometry +1187+1777 -composite -density 300 
Sample_no_crop.jpg

In the second command, you want to switch between "NorthWest" and "Center", so I would do that explicitly.

Code: Select all

magick convert 
-units PixelsPerInch -size 2362x3543 xc:white 
( ADH_20170808_5539.jpg -auto-orient -resize "2362x1766^" -gravity Center -crop 2362x1766+0+0 +repage ) 
-gravity NorthWest -geometry +0+0 -composite 
( ADH_20170808_5578.jpg -auto-orient -resize "1175x1766^" -gravity Center -crop 1175x1766+0+0 +repage ) 
-gravity NorthWest -geometry +0+1777 -composite 
( ADH_20170808_5619.jpg -auto-orient -resize "1175x1766^" -gravity Center -crop 1175x1766+0+0 +repage ) 
-gravity NorthWest -geometry +1187+1777 -composite -density 300 
Sample_crop.jpg
snibgo's IM pages: im.snibgo.com
32d2d7ee
Posts: 11
Joined: 2016-09-19T01:00:08-07:00
Authentication code: 1151

Re: convert & resize & crop & composite : unexpected results

Post by 32d2d7ee »

Thanks lot!
Indeed it was the missing -gravity NorthWest which caused the images to appear at the "wrong" place.
Post Reply