Embarrassingly simple question :?

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
smaines
Posts: 5
Joined: 2010-05-10T21:43:41-07:00
Authentication code: 8675308

Embarrassingly simple question :?

Post by smaines »

My ambition was brute simple: reverse the blue and green channels in an image. Gnome users may recognize its file name blue_type.png as a desktop background image.

I tried several variations, none of which seemed correct. Also, these two inexplicably yielded different results,

Code: Select all

$ convert blue_type.png -channel b -fx "u.g" -channel g -fx "u.b" green_type.png
$ convert blue_type.png -channel g -fx "u.b" -channel b -fx "u.g" green_type.png
I also tried (again inexplicably different results),

Code: Select all

$ convert blue_type.png blue_type_2.png -channel b -fx "v.g" -channel g -fx "v.b" green_type.png
$ convert blue_type.png blue_type_2.png -channel g -fx "v.b" -channel b -fx "v.g" green_type.png
...just in case u refers to the in-process clone (?!). Also I tried,

Code: Select all

$ convert blue_type.png -channel gb -fx "channel(r,b,g,a)" green_type.png
And many other things too silly to recall here.

What I expected to work was something like,

Code: Select all

$ convert blue_type.png  -fx "out.g = in.b;out.b = in.g"  green_type.png
But there seem to be no such lvalue semantics in -fx.

I am in awe of what this package does, and very embarrassed to be falling down at the first hurdle in trying to use it. What am I missing here?

Thanks in advance,

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

Re: Embarrassingly simple question :?

Post by fmw42 »

try

convert image -alpha off -colorspace rgb -separate -swap 1,2 -combine -alpha on result


For example:

convert -size 100x100 xc:blue -alpha off -colorspace rgb -separate \
-swap 1,2 -combine -alpha on tmp.png

will convert a blue image into a green image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Embarrassingly simple question :?

Post by snibgo »

Code: Select all

$ convert blue_type.png -channel b -fx "u.g" -channel g -fx "u.b" green_type.png
Should first set the blue channel to be equal to the green, then set the green channel to be equal to the new blue channel. It's like trying to swap the value in two variables by:

Code: Select all

a=b;
b=a;
which won't work.

Try:

Code: Select all

convert blue_type.png \( +clone -channel G -fx B \) \
  +swap -channel B -fx v.G     green_type.png
See http://www.imagemagick.org/Usage/transform/

about half way down (search for "swap the").
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: Embarrassingly simple question :?

Post by fmw42 »

in general -fx is slower that just using non-fx commands such as -separate and -combine.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Embarrassingly simple question :?

Post by snibgo »

Yes, as mentioned on that transform page.

convert blue_type.png -separate -swap 1,2 -combine green_type.png
snibgo's IM pages: im.snibgo.com
smaines
Posts: 5
Joined: 2010-05-10T21:43:41-07:00
Authentication code: 8675308

Re: Embarrassingly simple question :?

Post by smaines »

I've just returned to my computer. Thank you all for your replies, I so needed the sanity check! =D

I have a few comments:

snibgo, I understand what you're saying about a=b;b=a, (given the apparent, regretable implicit behavior of u), but then why doesn't this one work?

Code: Select all

    $ convert blue_type.png blue_type_2.png -channel b -fx "v.g" -channel g -fx "v.b" green_type.png
    $ convert blue_type.png blue_type_2.png -channel g -fx "v.b" -channel b -fx "v.g" green_type.png
Generally, though, shouldn't the solution to this problem have simpler expression within the semantics of -fx? Even if -separate,-swap,-combine is more efficient in this case, it is not intuitive, really, nor is resorting to -clone. (I confess I skipped over that part of the Transforms page, thinking it was too complex to be the right answer for something so simple). It seems like -fx is begging for a predefined symbol for "out"- and no funny business with u1 being implicitly mutable! Just an observation.

I'm looking forward to exploring ImageMagick further. Cool stuff!

Thanks again for all your help,

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

Re: Embarrassingly simple question :?

Post by snibgo »

I think separate/combine, and even clone/swap/delete become intuitive with use. I once worked as a draughtsman/tracer, which included sliding transparent sheets around and copying from one to another. Layers in Gimp, and image sequences in IM, work in much the same way.

I use "fx" only as a last resort. It is slow and not a general programming language. For example, I can have a conditional, but only one.


If blue_type_2.png is a copy of blue_type.png, this will work:

Code: Select all

convert blue_type_.png blue_type_2.png -channel B -fx v.g -channel g -fx v.b -delete 1 green_type.png
This is what you had, but with the addition of "-delete 1". This command creates a sequence of image zero and image one. Then it modifies the blue and green channels of image zero. Then it removes image one, so image zero is copied to the output. Without that delete, it would copy the unmodified image one.

(We don't need to create blue_type_2.png. We can just use +clone.)
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: Embarrassingly simple question :?

Post by anthony »

NOTE: -fx removes all images except for the 'result' image. Please keep this in mind!

As such the correct -fx solution for swapping green and blur channels is

Code: Select all

  convert rose: \( +clone -channel G -fx B \) \
          +swap -channel B -fx v.G     fx_gb_swap.gif

Another way of 'swapping' image channels is to use a -recolor (or -color-matrix) matrix.
however I have yet to review the changes to these options after the breif wave of 'fixes' with color matrix handling. (I was very busy at that time)

The simplest method for speed and understandability is the -separate -swap -combine technique.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Embarrassingly simple question :?

Post by snibgo »

Thanks Anthony.
NOTE: -fx removes all images except for the 'result' image. Please keep this in mind!
I don't understand. Do you mean that in:

convert blue_type_.png blue_type_2.png -channel B -fx v.g -channel g -fx v.b -delete 1 green_type.png

the first fx will remove image 1 (blue_type_2.png), so the second fx won't work, and the "-delete 1" should do nothing? Because this command does work. (Of course, it isn't the best way of doing the job.)
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Embarrassingly simple question :?

Post by snibgo »

Yes, the documentation (http://www.imagemagick.org/Usage/transform/ ) says "The command takes a image sequence of as many input images you like. Typically one or two images, and replaces ALL the input images with a copy of the first image, which has been modified by the results of the "-fx" function."

So the command I gave shouldn't work.
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: Embarrassingly simple question :?

Post by anthony »

This is why my 'channel swap' example (above) uses parenthesis and a clone. so that the original image is preserved.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply