Why is the Default Gravity Not The Same As Northwest?

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
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Why is the Default Gravity Not The Same As Northwest?

Post by fmw42 »

.
This is a note of caution for users who use -annotate. Annotating without setting a gravity may not produce what you want, since the default -gravity (-gravity none or +gravity) is not the same as -gravity northwest. This is something that I always thought until now was the same.

When I composite a text image onto a background, the default -gravity and -gravity northwest are the same.

Input:
Image

Default:

Code: Select all

composite label:SomeText -geometry +10+10 \
rings.jpg gravity_default_pos.jpg
Image

Northwest:

Code: Select all

composite label:SomeText -gravity northwest -geometry +10+10 \
rings.jpg gravity_northwest_pos.jpg
Image


But when I use -annotate, they differ:

Default:

Code: Select all

convert rings.jpg \
-annotate +10+20 'SomeText' \
gravity.jpg
Image


None:

Code: Select all

convert rings.jpg \
-gravity None -annotate +10+20 'SomeText' \
gravity_none.jpg
Image

The two above are the same, but ...

Northwest:

Code: Select all

convert rings.jpg \
-gravity NorthWest -annotate +10+20 'SomeText' \
gravity_northwest.jpg
Image


Both:

Code: Select all

convert rings.jpg \
-gravity NorthWest -annotate +10+20 'NorthWest' \
-gravity None      -annotate +10+20 'None' \
gravity_text_pos.jpg
Image


For reference see Anthony's page at
http://www.imagemagick.org/Usage/annota ... vity_image
and
http://www.imagemagick.org/Usage/annota ... avity_text

Where he states:

"The reason for the distinction is to ensure that IM text drawing remains compatible with other vector drawing image formats like the "SVG". These formats do not use gravity, so turning on gravity tells IM to follow the same rules as image placement when doing text drawing, rather than the vector graphics rules, involving the font 'baseline' and 'start' point of the text"

Code: Select all

Version: ImageMagick 6.9.7-0 Q16 x86_64 2016-12-18 http://www.imagemagick.org
Copyright: Copyright 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms lqr ltdl lzma openexr png ps rsvg tiff webp x xml zlib
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Why is the Default Gravity Not The Same As Northwest?

Post by anthony »

Annotate was created so as to avoid the need for 'doubled quoting', when using Draw.

See Text to Image Handling, Annotate
http://www.imagemagick.org/Usage/text/#annotate

Without gravity, the text position is the most logical method. Position the 'cursour' or 'caret' of the text before rendering it.
That is the position is on the 'baseline' of the text, and not its bounding box.

That means you position text on a 'write here line', regardless of the text's font, point size, density or other properties.

Gravity changes position to the bounding box of the text so it is handled in much the same way as placing an image. But you cannot control the 'baseline' position of the font. That information will be 'lost. This will makw aligning text with other text from other fonts and point sizes much, much more difficult.

See examples of base line alignment in
Creating Lines of Mixed Font Styles
http://www.imagemagick.org/Usage/text/#mixed_font_lines
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply