Problem merging 2 convert commands...

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
The Transporter
Posts: 14
Joined: 2007-01-15T13:54:49-07:00

Problem merging 2 convert commands...

Post by The Transporter »

Hello,

I'm looking for a new signatures for my photos and I made a news script based on what I saw on those 'so helpful' forums and I would like to improve my script.

Here is the code:

Code: Select all

convert $IMAGE \( +clone -blur 0x10 +level 20%,100% -fill black -colorize 30% \) \
          \( +clone -gamma 0 -shave "$BORDERWIDTH"x"$BORDERWIDTH" \
             -bordercolor white -border "$BORDERWIDTH"x"$BORDERWIDTH" \) \
          -composite \
          \( +clone -gamma 0 -shave "$BORDERWIDTH"x"$BORDERWIDTH" \
             -bordercolor white -border 1x1 \
             -bordercolor black -border "$tmpw"x"$tmpw" \) \
          -compose screen -composite \
          $IMAGE

convert -background '#0000' -fill white -gravity East -size ${width}x$RECTWIDTH \
        -font $FONT -strokewidth $STROKEWIDTH -pointsize $POINTSIZE caption:"`echo $CAPTION`" \
        +size $IMAGE +swap -gravity south -composite $IMAGE
It produces such résults:

Image

I'm using 2 commands 'convert' to do what I want as you can see and I'm wondering if it is possible to 'merge' them and have the same result of course !

And also do you think it's possible also to use mogrify instead of convert ?

Thanks !
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem merging 2 convert commands...

Post by anthony »

mogrify Only if you can avoid generating and compositing an extra image.
which is imposible the way you have it due to the composition blur.
But you can DIY a convert loop to do a directory of images.
http://www.imagemagick.org/Usage/basics/#mogrify_not

You may be able to if you replace the blurred composition, with a compose using the new 'variable blur mask' image.
http://www.imagemagick.org/Usage/compose/#blur
This is NEW, only present in the very latest releases.

Actually it may

As it is fixed it can be a separate image which you can then be applied with the -draw compose method. That is the only compose method that works with "mogrify" as the second image MUST be an external image, and thus a fixed image. Another fixed external image with a draw compose can overlay a semit-transparent black, ir a 'light' image to draken the blurred edge/

As for the merger of the two commands I don't see why you can't, as long as you already know the 'width' of the image. The second convert command generates a word wrapped caption, though from the example a label: would work just as well.
You would just generate the caption in a parenthesis, then compose it into the space generated.

Rather than generate that space separately however I would simply append the generated caption image!

For a mogrify version you again need to avoid creating a second image, You create a 'black' space, but then do a 'annotate' into that space. Unfortunately that means you have to use a 'fixed' pointsize, rather than getting IM to work out an appropriate pointsize to fit the black space :-( That I think my be the biggest killer, for a mogrify version.


Now go for it!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
The Transporter
Posts: 14
Joined: 2007-01-15T13:54:49-07:00

Re: Problem merging 2 convert commands...

Post by The Transporter »

anthony wrote:mogrify Only if you can avoid generating and compositing an extra image.
which is imposible the way you have it due to the composition blur.
But you can DIY a convert loop to do a directory of images.
http://www.imagemagick.org/Usage/basics/#mogrify_not
Yes this is already what I'm doing...

Here is the command I use:

Code: Select all

for i in *.jpg; do ./sig3.sh $i; done
anthony wrote: You may be able to if you replace the blurred composition, with a compose using the new 'variable blur mask' image.
http://www.imagemagick.org/Usage/compose/#blur
This is NEW, only present in the very latest releases.
It seems to be very interesting but it need a mask.
As a photographer, I don't have time to make those mask. When I do wedding pictures, people want them as fast as possible and I cannot afford to play with masks and so on. That's why my actual solution is terrific, but very slow :(
anthony wrote: Actually it may

As it is fixed it can be a separate image which you can then be applied with the -draw compose method. That is the only compose method that works with "mogrify" as the second image MUST be an external image, and thus a fixed image. Another fixed external image with a draw compose can overlay a semit-transparent black, ir a 'light' image to draken the blurred edge/

As for the merger of the two commands I don't see why you can't, as long as you already know the 'width' of the image. The second convert command generates a word wrapped caption, though from the example a label: would work just as well.
You would just generate the caption in a parenthesis, then compose it into the space generated.

Rather than generate that space separately however I would simply append the generated caption image!
I don't get you :) can you explain that part a bit more ? can you show me an example of this ? Thanks alot !
anthony wrote: For a mogrify version you again need to avoid creating a second image, You create a 'black' space, but then do a 'annotate' into that space. Unfortunately that means you have to use a 'fixed' pointsize, rather than getting IM to work out an appropriate pointsize to fit the black space :-( That I think my be the biggest killer, for a mogrify version.
I wanted to use mogrify because I don't create new images, I modify those existing.
If it's not possible it's not a problem, convert will do it also :)
for the pointsize, I don't get you, you mean that I can automatically get the text to fit into the box with specifying the pointsize ? That would be really really really great !!!
anthony wrote:Now go for it!
Yes yes that's what I'm doing :)

I already made 5 scripts, those 5 scripts are very useful and I would like to improve them and be as good as you when I see your examples!

Just for your information, this is the kind of script I do:

Code: Select all

#!/bin/sh -x
# http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=12114&start=15

IMAGE=$1
CAPTION="La Place De Mons"
POINTSIZE="68"
STROKEWIDTH="0"
BORDERPC="3"
RECTWIDTH="100"
RESIZETO="1024"

FONT="./comics.pfb"

width=`identify -format "%w" $IMAGE`
heigth=`identify -format "%h" $IMAGE`

BORDERWIDTH=$( echo "scale=4;($BORDERPC/100*$width)"|bc)

tmpw=$( echo "scale=4;($BORDERWIDTH-1)"|bc)

convert $IMAGE \( +clone -blur 0x10 +level 20%,100% -fill black -colorize 30% \) \
          \( +clone -gamma 0 -shave "$BORDERWIDTH"x"$BORDERWIDTH" \
             -bordercolor white -border "$BORDERWIDTH"x"$BORDERWIDTH" \) \
          -composite \
          \( +clone -gamma 0 -shave "$BORDERWIDTH"x"$BORDERWIDTH" \
             -bordercolor white -border 1x1 \
             -bordercolor black -border "$tmpw"x"$tmpw" \) \
          -compose screen -composite \
          $IMAGE

convert -background '#0000' -fill white -gravity East -size ${width}x$RECTWIDTH \
-font $FONT -strokewidth $STROKEWIDTH -pointsize $POINTSIZE caption:"`echo $CAPTION`" \
+size $IMAGE +swap -gravity south -composite $IMAGE

mogrify -strip -quality "85%" -resize "${RESIZETO}x" $IMAGE
What do you think ?

Thanks for you precious help and time !
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Problem merging 2 convert commands...

Post by Bonzo »

If you are trying to save time on lots of images I would try and find the image dimensions out of ImageMagick. Its only microseconds but:
ImageMagick identify
Width = 4000 - Time = 0.144232034683
php getimagesize()
Width = 4000 - Time = 0.00023889541626
I see you are resizing at the end - could you not resize earlier?

Another speed tip using convert and jpgs only is to add a size to the code. I do not know if it works with mogrify.

Code: Select all

convert -size 100x100 input.jpg -resize 100x100 output.jpg
This can cut the resize time by 1/2 - http://www.rubblewebs.co.uk/TESTS/results.html
The Transporter
Posts: 14
Joined: 2007-01-15T13:54:49-07:00

Re: Problem merging 2 convert commands...

Post by The Transporter »

Bonzo wrote:If you are trying to save time on lots of images I would try and find the image dimensions out of ImageMagick. Its only microseconds but:
ImageMagick identify
Width = 4000 - Time = 0.144232034683
php getimagesize()
Width = 4000 - Time = 0.00023889541626
I see you are resizing at the end - could you not resize earlier?

Another speed tip using convert and jpgs only is to add a size to the code. I do not know if it works with mogrify.

Code: Select all

convert -size 100x100 input.jpg -resize 100x100 output.jpg
This can cut the resize time by 1/2 - http://www.rubblewebs.co.uk/TESTS/results.html
All that stuff is really interesting ! Thanks !

I think i'll rewrite everything in php sooner or later, I don't like using bash.

I'll try those tips tonight @ home and report the results ;-)

Thanks !
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Problem merging 2 convert commands...

Post by Bonzo »

A php example but you should be able to modify it :D

Showing how to use append together with caption - I kept with the identify method of getting the width for now.

Code: Select all

<?php  
 $IMAGE = "th_IMG_0550.jpg";
 $CAPTION = "La Place De Mons";
 $WIDTH = exec("identify -format \"%w\" $IMAGE");
 $FONT = "DRAGM.ttf";
 
$cmd = " $IMAGE ( -size {$WIDTH}x130 -background black -fill white -font $FONT -pointsize 25 -gravity center caption:\"$CAPTION\" -flatten ) -append ";
 
exec("convert $cmd caption.jpg");
?> 

<img src="caption.jpg">
Post Reply