How to use -set

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
djaquay
Posts: 6
Joined: 2016-01-25T18:51:47-07:00
Authentication code: 1151

How to use -set

Post by djaquay »

I'm trying to use convert to append two images. I want the 2nd image to match the width of the first. If I do this:

Code: Select all

convert fsr.jpg \( watermark.jpg -resize 480 \) -append -verbose fsr-marked.jpg
it works fine. Except I won't always know the width of the first image. So I try:

Code: Select all

convert fsr.jpg -set option:davewidth '%w' \( watermark.jpg -resize %[davewidth] \) -append -verbose fsr-marked.jpg
and even though davewidth shows up in info:, set to 480, watermark.jpg isn't resized. What am I missing here? Is there a better technique? (Yes, I'm very new to ImageMagick...)

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

Re: How to use -set

Post by snibgo »

"-resize" needs actual numbers. These could be from environment variables, of course. "-distort SRT" can use fx expressions. "davewidth" is string. As far as I know, it can't be used as a number in an fx expresson.

I think the only way is to get the size of first image, then use that for the resizing.
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: How to use -set

Post by fmw42 »

I believe that IM 7 will allow resize to use fx expressions
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to use -set

Post by GeeMack »

djaquay wrote:I'm trying to use convert to append two images. I want the 2nd image to match the width of the first. [...] What am I missing here? Is there a better technique?
If you're willing to move into ImageMagick v7, your command may work just the way you're trying to do it. I have beta version "ImageMagick 7.0.0-0 Q16 x64 2016-01-17" installed on a Windows 7 64 machine, and the "-resize %[davewidth]" uses that "-set option:davewidth" variable just fine. I've been using those same types of operators in several scripts for a couple months without any problems.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to use -set

Post by GeeMack »

fmw42 wrote:I believe that IM 7 will allow resize to use fx expressions
I was writing my reply while you were posting this.

My experience with IM7 so far shows that an FX expression of the construction "%[fx:w+h]" will work almost anywhere, and can include most of the symbols described on THIS PAGE.

And the "-set option:varname value" variable will work almost anywhere when called back up like this "%[varname]".

The thing that doesn't work yet is putting the "-set option:..." variable inside a "%[fx:...]" expression.

This works...

Code: Select all

magick input.jpg \( watermark.jpg -resize %[fx:100+200] \) -composite output.jpg
And this works...

Code: Select all

magick input.jpg -set option:my_w %[w] \( watermark.jpg -resize %[my_w] \) -composite output.jpg
But this doesn't work...

Code: Select all

magick input.jpg -set option:my_w %[w] \( watermark.jpg -resize %[fx:my_w+200] \) -composite output.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use -set

Post by fmw42 »

magick input.jpg -set option:my_w %[w] \( watermark.jpg -resize %[fx:my_w+200] \) -composite output.jpg
I do not know the limitation of using -fx expressions, but for this one, why not just do

Code: Select all

magick input.jpg -set option:my_w %[fx:w+200] \( watermark.jpg -resize %[fx:my_w] \) -composite output.jpg
String formats such as %w do not allow computations for but the equivalent fx expressions do.
djaquay
Posts: 6
Joined: 2016-01-25T18:51:47-07:00
Authentication code: 1151

Re: How to use -set

Post by djaquay »

Ok, thanks, I'll look into FX expressions.

I'm willing to upgrade to v7. I'll be running this on a Mac. I'm not seeing that on the downloads page, would MacPorts support building that version? (I'm really new to Macs, doing this for my wife, so all pointers for this would be greatly appreciated).

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

Re: How to use -set

Post by GeeMack »

djaquay wrote:I'm willing to upgrade to v7. I'll be running this on a Mac. I'm not seeing that on the downloads page, would MacPorts support building that version? (I'm really new to Macs, doing this for my wife, so all pointers for this would be greatly appreciated).
It's not even so much about the FX expressions for what you described, but to use those "-set option:variable" variables in your resize operators, etc., can be quite useful.

The downloads for IM7 can be found from the "Downloads" link on the top bar of THIS PAGE. I don't know about Mac version compatibility and all, but it looks like there are some fairly detailed instructions for installing using MacPorts on that download page.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use -set

Post by fmw42 »

I do not believe that MacPorts supports IM 7, yet. Here is how I install IM on my Mac by using MacPorts to install all my delegates and then install IM 6 or 7 from source. see viewtopic.php?f=1&t=21502&p=88202&hilit ... rts#p88202

Change your configure command as desired for what to enable and disable, but keep the blue commands there.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to use -set

Post by GeeMack »

fmw42 wrote:I do not know the limitation of using -fx expressions, but for this one, why not just do

Code: Select all

magick input.jpg -set option:my_w %[fx:w+200] \( watermark.jpg -resize %[fx:my_w] \) -composite output.jpg
Yep, that's how I usually handle it when the situation is that straightforward. It can be trickier when I'm wanting to do other calculations using that value further along in the script, like maybe after a "-trim" operation or sizing to percentages where the results aren't necessarily known. It's usually do-able, but sometimes it means working far enough ahead to establish the values for those "-set option:..." variables before the %w or %h change for instance.
djaquay
Posts: 6
Joined: 2016-01-25T18:51:47-07:00
Authentication code: 1151

Re: How to use -set

Post by djaquay »

Sounds good, thanks. Out of curiosity, when will v7 be in full release? Or is there a similar way to do what I want in v6?

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

Re: How to use -set

Post by fmw42 »

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

Re: How to use -set

Post by GeeMack »

djaquay wrote:Or is there a similar way to do what I want in v6?
I use a *nix emulator running the "bash" shell, which should be the default shell on most current Macs. The code below should get you what you want by using a shell variable as fmw42 suggested..

Code: Select all

IMG="input.jpg"

WATERMARK="watermark.jpg"

WIDTH=`convert "${IMG}" -format %w info:`

convert "${IMG}" \( "${WATERMARK}" -resize "${WIDTH}"x \) -append "${IMG%.*}-marked.jpg"
That starts by setting the value of the variable "IMG" to your input image and the value of "WATERMARK" to your watermark image.

Then it sets the variable "WIDTH" to the output of the command between the backticks. It's a "convert" command that just requests information from the "IMG" file and provides the width.

Then the next "convert" command starts with the input "IMG", brings in the "WATERMARK" inside the parentheses, resizes it to "WIDTH" (and whatever height), and appends it to the bottom of the input "IMG".

The output file is named from the input file without the extension, followed by "-marked.jpg".

I tried a "for" loop like this to convert all the JPG files in a directory, and it worked just as expected.

Code: Select all

WATERMARK="watermark.png"

for IMG in *.jpg ; do
   WIDTH=`convert "${IMG}" -format %w info:`
   convert "${IMG}" \( "${WATERMARK}" -resize "${WIDTH}"x \) -append "${IMG%.*}-marked.jpg"
done
For this loop I made the watermark image a PNG to avoid making another copy of it with a watermark attached. You could keep it as a JPG and use the full path to call it in from another directory, of course.

EDITED TO ADD: I suppose the original idea can be done as a single line *nix shell command without using any variables, but it requires typing that file name a bunch of times. The command between the backticks inside the parentheses will obtain and use the width of the "fsr.jpg" file to resize the "watermark.jpg" before appending it. Like this...

Code: Select all

convert fsr.jpg \( watermark.jpg -resize `convert fsr.jpg -format %w info:`x \) -append fsr-marked.jpg
djaquay
Posts: 6
Joined: 2016-01-25T18:51:47-07:00
Authentication code: 1151

Re: How to use -set

Post by djaquay »

Sorry for bumping an old thread, but I *finally* got back around to working on this again, and GeeMack's final reply was exactly what I needed. I put that into an OSX services script, and (after a little bit of wrestling with PATH and exports and such) it's working great.

Thanks so much, GeeMack and everybody else, for helping me figure this out.

Dave
Post Reply