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

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
ImageCropper
Posts: 2
Joined: 2014-07-21T17:30:43-07:00
Authentication code: 6789

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

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

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

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

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

Post 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.
ImageCropper
Posts: 2
Joined: 2014-07-21T17:30:43-07:00
Authentication code: 6789

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

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

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

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

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

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

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

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

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

Post by fmw42 »

P.S. -ping must come before the image in the command line
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply