Page 1 of 1

Gradient masks - is it possible?

Posted: 2016-05-10T11:04:08-07:00
by aehtyb
I know Imagemagick supports 'variable blur mapping' which lets you supply an image + gradient, and the image is blurred using the gradient as a mask to determine blur intensity. I've looked through as much documentation as I could but can't seem to find a way to use a 'variable mapping' procedure for transforms other than blurring.

I am curious if it is possible to use a gradient as a mask for the built-in distortion transformations.. such as the pincushion 'barrel' distortion, but only in a specified region with variable intensity, like the example below:

original image:
Image

gradient mask:
Image

desired result:
Image

This is something that could be done with ease in Photoshop--selecting the center region with the elliptical marquee tool, and then 'feathering' the selection by 15px or so, and applying the distortion. (Or even doing it exactly as above using a gradient as a layer mask, like the example above.)

Simply cropping out the region of interest, distorting it, then replacing it back into the original image in the same spot doesn't work, because of issues with alpha blending on the edges (im using an image that has transparency in it). Any advice would be much appreciated.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T11:25:52-07:00
by snibgo
I would do this with absolute distortion maps.

1. Create an identity distortion map.

2. Distort this map in whatever way you want.

Now you have two distortion maps. If you applied (1) to your image, it wouldn't change. If you applied (2), the entire image would be distorted.

3. From those, create a third distortion map, using your gradient as a mask:

Code: Select all

convert map1.png map2.png grad.png -composite map3.png
Then use map3.png as your distortion map.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T15:04:31-07:00
by snibgo
Naming your first two images inGrid.png and inMask.png, and using a heavy barrel distortion for clarity, Windows BAT syntax:

Code: Select all

set IDENT=^
0,0,#008,^
%%[fx:w-1],0,#f08,^
0,%%[fx:h-1],#0f8,^
%%[fx:w-1],%%[fx:h-1],#ff8

%IM%convert ^
  inGrid.png ^
  ( +clone ^
    ( +clone ^
      -sparse-color bilinear "%IDENT%" ^
    ) ^
    ( +clone ^
      -distort Barrel 0,0,-0.5 ^
    ) ^
    -delete 0 ^
    inMask.png ^
    -compose Over -composite ^
  ) ^
  -compose distort -composite ^
  outMaskedBarrel.png
Image

Re: Gradient masks - is it possible?

Posted: 2016-05-10T15:34:19-07:00
by aehtyb
snibgo, thanks for your suggestions - I should have actually uploaded the PNG of the grid I was using rather than taking a screenshot (the grid has a transparent background) so using the batch script produces an ghost image underneath.. e.g.

Image

Using masks in photoshop definitely provides 'variable' intensities of the distortion effect in relation to how much the mask tells it to, which is why I was hopeful Imagemagick might have something comparable considering it has that variable blur mapping. I was confused by your original answer - sorry.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T15:43:37-07:00
by snibgo
So, please upload the grid and gradient you actually want to use. (My script should work with them.)

Re: Gradient masks - is it possible?

Posted: 2016-05-10T15:59:55-07:00
by aehtyb
Here is the grid & mask.

http://doir.ir/inGrid.png
http://doir.ir/inMask.png

I processed the images above through the batch script, and the image I posted a moment ago was the result.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T16:08:32-07:00
by fmw42
The problem is that your grid is in the alpha channel and the underlying image is totally black. Put the alpha channel into the underlying image first and see if that works.

Code: Select all

convert inGrid.png -background white -alpha background inGrid2.png

Re: Gradient masks - is it possible?

Posted: 2016-05-10T17:01:29-07:00
by aehtyb
Just attempted to run the above command - but the inGrid2.png output seemed to be the same thing as the input (inGrid.png).. no? at any rate - I tried to run the batch file again with inGrid2.png as the input and it produced that same ghost image I posted above.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T17:13:22-07:00
by snibgo
As Fred says, it is the alpha channel that needs to be distorted. (The colour isn't entirely black, but a very dark weird pattern. Never mind.)

Code: Select all

%IM%convert ^
  inGrid.png ^
  -alpha extract ^
  p.png
This makes a grayscale image of the alpha. Put that through the code I gave above. Then CopyOpacity to put the result as the alpha channel in the original image, or on an entirely black image.

Or, if you want the weird almost-black pattern distorted, then distort that as well as the alpha channel.

Re: Gradient masks - is it possible?

Posted: 2016-05-10T19:23:25-07:00
by snibgo
Or we can do the whole job in a single convert:

Code: Select all

%IM%convert ^
  inGrid2.png ^
  ( +clone ^
    -alpha opaque ^
    ( +clone ^
      -sparse-color bilinear "%IDENT%" ^
    ) ^
    ( +clone ^
      -distort Barrel 0,0,-0.5 ^
    ) ^
    -delete 0 ^
    inMask2.png ^
    -alpha off ^
    -compose Over -composite ^
  ) ^
  -compose distort -composite ^
  ingrid_out2.png
(%IDENT% is as above, using a variable purely for convenience.)

This uses an opaque image to create a distortion map, then uses the map to distort both colours and alpha together.
Image

Re: Gradient masks - is it possible?

Posted: 2016-05-10T19:40:53-07:00
by aehtyb
man this is great - this works beautifully! Thank you very much for the help. Now I've gotta dissect and figure out exactly what's goin on in this batch script exactly ;). Thanks again.