-geometry widthxheight<

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
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

-geometry widthxheight<

Post by whugemann »

At https://imagemagick.org/script/command- ... p#geometry is says that the modifier < enlarges an image with dimension(s) smaller than the corresponding width and/or height argument(s).

I am trying to rectify a perspective photograph with ImageMagick in a CMD batch file under Windows, picking the image coordinates from the photograph and supplying the world coordnates in centimeters. I have to extend the canvas if the world coordinates exceed the image dimensions:

Code: Select all

magick %1 -gravity northwest -extent %EXT% -virtual-pixel white -distort perspective "%B1% %W1% %B2% %W2% %B3% %W3% %B4% %W4%" -trim -flip test.jpg
Where %B1% ... %B4% are the pixel coordinates of the original photograph and %W1% .. %W4% are the world coordinates, i.e. the (rational) pixel coordinates of the target image.

I set the %EXT% paramter to the larger dimensions of the original photo and the target image: If the original photograph has for example 1000x2000 pixel and the target image needs to be 500x3000 pixel, I would like to set %EXT% to 1000x3000 pixel.

I did not succeed by using the modifyer -extent 500x3000< and thus performed the task of setting %EXT% correctly within the CMD batch file.

Could you think of a more elegant way to do the trick?
Wolfgang Hugemann

User avatar
fmw42
Posts: 26094
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -geometry widthxheight<

Post by fmw42 »

I am not sure I understand your question. But using +distort rather than -distort will expand the canvas automatically without the need to enlarge your input. See the comment about +distort at https://imagemagick.org/Usage/distorts/#box3d

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

Re: -geometry widthxheight<

Post by snibgo »

Not all operations with {geometry} can use modifiers such as "<".

You might make a xc:None image of the %EXT$ size, and composite your input over that:

Code: Select all

magick -size %EXT% xc:None -gravity Center %1 -composite ...
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

fmw42 wrote:
2019-12-05T11:40:45-07:00
But using +distort rather than -distort will expand the canvas automatically without the need to enlarge your input.
My results with +distort perspective are not very promising. Try:

Code: Select all

magick -size 300x200 xc:white -draw "polyline 50,180 260,180 120,80 100,80" rect.png
magick rect.png -virtual-pixel white                  +distort perspective "50,180 50,50 260,180 260,50 120,80 260,1000 100,80 50,1000"  rect_p.png
magick rect.png -virtual-pixel white -extent 300x1100 -distort perspective "50,180 50,50 260,180 260,50 120,80 260,1000 100,80 50,1000"  rect_m.png
In the first line, I paint a perspective view of a road, which I then want to rectify. This works as expected with -distort and extent (I skipped the flip), but yields a strange result with +distort.
Wolfgang Hugemann

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

snibgo wrote:
2019-12-05T11:51:51-07:00
Not all operations with {geometry} can use modifiers such as "<".
I think this is a general "misbehaviour" of this modifier. Try

Code: Select all

magick -size 300x200 xc:yellow -resize 200x400< rs.png
It leaves the image at 300x200 pixel, whereas I would expect it to have 300x400 after resizing.
(In the Windows cmd-box, I escaped the < with a caret, i.e. ^<, right?)
Wolfgang Hugemann

User avatar
fmw42
Posts: 26094
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -geometry widthxheight<

Post by fmw42 »

It resizes to 300x200 since that is the input and it is larger in one dimension than the corresponding resize value and the < alone preserves aspect ratio. To do as you want, you need to ignore aspect by adding the ! symbol. So try

Code: Select all

magick -size 300x200 xc:yellow -resize 200x400\<\! rs.png

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

Thank you, Fred,

Code: Select all

magick -size 300x200 xc:yellow -resize 200x400^<! rs.png
does the trick I need here under Windows. I did not know that you can combine the modifiers. Is that mentioned anywhere? Surely not in the corresponding section of Anthony's Usage pages (?).
Wolfgang Hugemann

User avatar
fmw42
Posts: 26094
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -geometry widthxheight<

Post by fmw42 »

I do not see it mentioned either. I will take note to add something.

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

Returning to my original problem after a few days off, I had to discover that the trick doesn't work with -extent, as

Code: Select all

magick -size 300x200 xc:yellow -extent 200x400^<! rs2.png
does not yield the same dimensions as

Code: Select all

magick -size 300x200 xc:yellow -resize 200x400^<! rs.png
With -extent it's still 200x400 pixels -- as one could have expected, as -extent doesn't car for the aspect ratio.

Thus I think that the handling of this modifier by -extent is slightly incorrect (?).
Wolfgang Hugemann

User avatar
fmw42
Posts: 26094
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -geometry widthxheight<

Post by fmw42 »

Not all operators use every modifier in the Geometry page. The list is mostly for -resize.

User avatar
GeeMack
Posts: 740
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: -geometry widthxheight<

Post by GeeMack »

whugemann wrote:
2019-12-16T08:36:23-07:00
With -extent it's still 200x400 pixels -- as one could have expected, as -extent doesn't car for the aspect ratio.
If you use a single argument and specify the output dimensions of "-extent" as a percent instead of a number of pixels, it will maintain the input's aspect ratio.

You're using IMv7, so you can do some handy inline calculations with FX expressions. This command will extend (or reduce) the viewport of the input image to the maximum of a 1000 pixel width or a 3000 pixel height, whichever is a larger percentage, and still keep the aspect ratio...

Code: Select all

magick wizard: -extent %[fx:max(1000/w,3000/h)*100)]% result.png
If you can determine those 1000 and 3000 dimensions and feed them to the command as variables, that might get close to what you're trying to do.

To use this in a Windows BAT script you'll have to double the percent signs "%%".

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

GeeMack wrote:
2019-12-16T12:44:56-07:00
magick wizard: -extent %[fx:max(1000/w,3000/h)*100)]% result.png
Your answer set me on the right track. The most efficient code for me is

Code: Select all

-extent "%%[fx:max(%IW%,%OW%)]x%%[fx:max(%IH%,%OH%)]"
where IW and IH are the input images width and height and OW and OH are the output images width and height.
So far, I did not know that you can use the fx operator twice within the square brackets.
Wolfgang Hugemann

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: -geometry widthxheight<

Post by whugemann »

Correction: The most efficient code is of cause

Code: Select all

-extent "%%[fx:max(w,%OW%)]x%%[fx:max(h,%OH%)]"
as you can address the input image's width and height directly.
However, this code does only work with IM 7. In my understanding the functionaly of fx has been extended with IM 7 (?).
Wolfgang Hugemann

User avatar
fmw42
Posts: 26094
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -geometry widthxheight<

Post by fmw42 »

IM 7 adds the ability to do inline fx computations. IM 6 did not have that except for a few methods such as -distort.

Post Reply