Page 1 of 1

Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T17:56:39-07:00
by ImageCropper
Hello, everybody! :)

I'm unable to get an FX-expression to work in combination with other commands, like "roll" or "crop".

For e.g., have a look at the following bash-script, where i try to roll an image of unknown(!) size 10% to the right:

Code: Select all

#!/bin/bash

IMAGE="in/rainbow.jpg"

ROLL=`convert "$IMAGE" -format '+%[fx:floor(page.width*0.1)]+0' info:-`

echo $ROLL
# Prints "+100+0" as expected. No problems so far

convert "$IMAGE" -roll "$ROLL" out/b.jpg
# This one works fine and rolls the image 10% to the right.

# Now combine the above two calls to one:
convert "$IMAGE" -roll '+%[fx:floor(page.width*0.1)]+0' out/a.jpg
# "a.jpg" looks exactly the same as our input image. nothing is rolled. Why?

convert --version
# My version is 6.6.9-7.
As you can see above, theoretically I could declare a ROLL variable containing my FX-expression I got with "-format", but that would mean, that I call the convert process two times, which is not very efficient in my opinion.

Actually this posted script is a heavily stripped down version of my real problem, and in reality I need much more values than this ROLL thing. If I would create a single variable every time - which means an extra process, which reads the whole image again and again - isn't it a waste of resources? :)

So finally, my question is as follows: How can I for e.g. roll my image around 10% horizontally, using an FX expression?

Thank you very much for reading, and I hope, someone will find an answer to my question.

Have a nice day! :)

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T17:59:52-07:00
by fmw42
You have to do it in two commands. Most of IM 6 does not recognize inline fx expressions. The main ones that do are related to -distort. IM 7 will be more flexible in that regard.

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T18:04:03-07:00
by fmw42
You can do the equivalent of -roll with inline fx expressions if you use -distort SRT. See http://www.imagemagick.org/Usage/distorts/#srt. However, to make it wrap, you need to use -virtual-pixel HorizontalTile or VerticalTile see convert -list virtual-pixel.

To do a crop inline, you would use +distort SRT with -define distort:viewport. see http://www.imagemagick.org/Usage/distor ... t_viewport

For both use -filter point before -distort.

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T18:13:38-07:00
by ImageCropper
Thank you very much for your comments so far!

But isn't the -distort SRT much(!) more heavy weight internally? A simple roll should be much more efficient, right? Or is SRT optimized for this kind of situations?

Anyway I will try your solutions now, and maybe I'll compile the 7 alpha later.

Again, thank you! And have a nice day! :)

Edit: "alpha", not "beta" :)

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T18:31:33-07:00
by fmw42
Yes, you are right. distort SRT is not optimized like -roll or -crop. The -filter point does help to make it faster and avoid blurring.

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T18:34:19-07:00
by fmw42
Using two command lines is not really adding too much time, since you are just reading the width and height. You can add -ping to identify or convert so that it only looks at the headers and does not read the whole image. see http://www.imagemagick.org/Usage/basics/#controls

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T18:37:24-07:00
by fmw42
I am not sure why you are using page.width. Do you have a virtual canvas with offsets? If not, you can get the image width or height from just w or h in fx. Other image parameters can be achieved with string format, but would require another fx expression to do computations on them. See http://www.imagemagick.org/script/escape.php

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T19:07:16-07:00
by fmw42
P.S. -ping must come before the image in the command line

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-21T22:06:06-07:00
by snibgo
From Windows cmd, we can call "convert" once and read the image once, to set multiple environment variables from multiple escapes or fx calculations.

I suspect this is also possible from bash, but I haven't figured out how.

Re: Unable to use FX-expressions in "roll", "crop", a.s.o.

Posted: 2014-07-22T17:27:23-07:00
by anthony
An example of generating 'calculated rolls' using Distort and FX expressions is in Animations

Animated Distorts - distorting multiple image based on image index
http://www.imagemagick.org/Usage/anim_mods/#distort

The key to making it roll is to use a tiling virtual pixel setting. That is defining what pixels outside the image proper should be.