Why does ImageMagick border gravity always go north instead ofsouth for captions?

Discussion in 'Digital Photography' started by Calia, May 24, 2014.

  1. Calia

    Calia Guest

    I'm trying to caption all the pictures in a directory.
    The first step is adding white space to the bottom.
    The alias below works, but it puts the 200-pixel white
    space at the TOP of the picture every time, no matter
    whether I have the "gravity" option set to "north" or

    I actually want the caption on the bottom! (south).

    What's wrong with my alias that the captions always
    end up on the north side?

    Any suggestion?

    alias caption='for f in *.{jpg,gif,JPG,GIF,jpeg,JPEG,png,PNG};do convert $f -splice 0x200 -gravity south -bordercolor white white_$f; mv white_$f $f;kolourpaint $f; done'
    Calia, May 24, 2014
    1. Advertisements

  2. Calia

    Lew Pitcher Guest

    Try putting the
    -gravity south
    option /before/ the
    -splice 0x200
    option, as in
    convert $f -gravity south -splice 0x200 -bordercolor white white_$f
    Lew Pitcher, May 24, 2014
    1. Advertisements

  3. Calia

    Calia Guest

    That worked!

    Now how the heck did you know that would work?

    There was no error message with my method.
    But, mine put the gravity north no matter what.
    Yet, yours put gravity south.

    How did you know that the options were order dependent?
    Calia, May 24, 2014
  4. http://www.imagemagick.org/script/command-line-options.php#gravity
    contains a hint about this "If gravity is applied before..."

    ?There should be no error in your method since it appears that it is
    correct-- it changes the gravity for any following commands.
    Ie, had you now had a command after your "gravity" command, it would
    have shifted it. It did not know you wanted to apply it to the prior
    William Unruh, May 24, 2014
  5. Calia

    Lew Pitcher Guest

    I followed the documentation...

    convert(1) ("man 1 convert") says
    convert - convert between image formats as well as resize an
    image, blur, crop, despeckle, dither, draw on, flip, join, re-sample,
    and much more.

    convert input-file [options] output-file

    For more information about the convert command, point your browser
    to file:///usr/share/doc/ImageMagick-6.4.3/www/convert.html or

    And http://www.imagemagick.org/script/convert.php says
    -splice geometry splice the background color into the image

    And, the link on "-splice"
    (http://www.imagemagick.org/script/command-line-options.php#splice) says
    This will add rows and columns of the current -background color into the
    given image according to the given -gravity geometry setting. >See Image
    Geometry for complete details about the geometry argument.

    From that, it became apparent (to me, at least) that you had to set
    the -gravity first, so that -splice wouhd have a "given -gravity geometry

    BTW, you want -background <colour>, not -bordercolor <colour>, and
    the -background option should precede the -splice option, just like
    the -gravity option did.

    Gravity defaults to north.
    Actions are executed sequentially, so the -splice option is
    executed /before/ the -gravity option in your original commmandline.

    I simply had you put an overriding -gravity option /before/ the -splice.
    Many commands have order-dependant options, so it is one of those things you
    look for. And, the documentation on -splice implied the dependence on
    order. So, that's what I tried.

    And, for what it's worth, I actually tried (for real) a number of
    combinations before coming up with one that worked.
    Lew Pitcher, May 25, 2014
  6. Calia

    Calia Guest

    Thanks to your astute advice, here's the current alias that I'm using:
    alias caption='for f in *.{jpg,jpeg,gif,png,JPG,JPEG,GIF,PNG};do convert $f -background white -gravity south -splice 0x200 tmp_$f; mv tmp_$f $f;kolourpaint $f; done'

    That alias:
    a) Finds all pictures in the current directory, and,
    b) Sets the color to white for the background to be added, and,
    c) Sets the location of the splice to be south (i.e., bottom), and,
    d) Adds a 200-pixel splice to the bottom of the picture (for captions), and,
    e) Saves the file, and then moves the file to the original name, and,
    f) Then opens up Kolourpaint which adds the actual desired caption.

    I find this useful to send batches of pictures to friends and
    family where *each* picture is annotated with the ad hoc storyline.

    Any improvements to the alias are *always* welcome!
    Calia, May 25, 2014
  7. Calia

    Aragorn Guest

    Personally, I'd recommend writing a function for this sort of thing.
    Aragorn, May 25, 2014
  8. Calia

    Calia Guest

    I've written (long ago) Fortran functions.
    Do you mean to suggest a c program that does this?
    Calia, May 25, 2014
  9. Calia

    Aragorn Guest

    No, a shell function. It's somewhat similar to an alias, but more
    flexible. Among other things, you can define local variables inside a
    function. An example follows.

    for i in *

    if [ some_condition ]

    exit 0

    You can write lots of functions like that and /source/ them from your
    shell initialization file - e.g. ~/.bashrc - or from another file. For
    instance, you can have a file ~/.myfunctionsrc and then in your
    ~/.bashrc you have a line like...

    source $HOME/.myfunctionsrc
    Aragorn, May 25, 2014
  10. Calia

    Aragorn Guest

    My bad. I'm used to writing functions for use inside of scripts, which
    are of course normally executed within a subshell. ;-)
    That would work, of course, but I think it may be cleaner to have a
    separate, modular file for that, just in case .bashrc ever gets
    overwritten. ;-)
    Aragorn, May 31, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.