Search code examples
imagemagick

make parts of edges soft with ImageMagick


Here is a drawing made to resemble an id photo of someone on transparent background. I'd like to make the bottom part soft. It must be the bottom including the sides for as long as they are touched by the green form. The -blur technique is softening the entire pattern, therefore i gave it up.

The idea would be to draw a rectangle at the bottom and fill it with gradient from black to none, (top : black, to bottom : none) and doing similarly with two small rectangles on the sides.

It seems gradient colours are not well supported on transparent background.

magick -size 120x120 xc:none -alpha set -stroke none \
  -fill SpringGreen2 -draw 'rectangle 0,100 120,120' \
  -fill SpringGreen3 -draw 'roundrectangle 20,60 100,120 10,10' \
  -fill SpringGreen -draw 'circle 60,40 60,65'  \
  out.gif

here is something that looks like the sought after goal, done in inkscape using a bezier with white colour and bluring it, so here on a white web page it looks good. It must be transparent background.

enter image description here

EDIT: actually in the original problem the input is an image file with transparent background, not something draw by imagemagick. Therefore I'd need a solution that handles a file an input.


Solution

  • magick -size 500x640 -background none xc:none -alpha set -stroke none \
       -draw 'fill magenta rectangle 0,640 500,560' \
       -draw 'fill SpringGreen3 roundrectangle 60,380 440,640 40,40' \
       -draw 'fill SpringGreen circle 250,300 250,380' \
       idphoto.png
    
    magick idphoto.png -write MPR:orig -alpha extract \
      \( +clone -colorspace Gray -fx white \
      -shave 1 -bordercolor black -border 1 -blur 0x12 -level 50,100% \) \
      -compose multiply -composite -write resultalpha.png \
      MPR:orig +swap -compose CopyOpacity -composite \
      "softedge_idphoto.png"
    

    1 - load the input image and save it with the dedicated MPR: method of ImageMagick for easy easy recall

    2 - New empty canvas, with the dimensions of the input image: +clone -fx white just a clone of any layer of the input image is fine (here alpha due to -extract alpha) and force fill with white, kind of full erase. There might other methods to do that... Thanks to the parentheses the commands will affect only the new canvas. Gray colorspace is what CopyOpacity method, used later, likes

    3 - create a black border and blur it with -shave 1 -bordercolor black -border 1 -blur 0x12' additionally with -level 50,100% to make sure that the edges are totally transparent. This is the 'soft edges' that is sought after

    4 - merge this new layer with the alpha layer of the original image. -composite uses the topmost two layers of the stack which here are the one created with +clone and the alpha layer of the input image, set active with -alpha extract. multiply method is self descriptive

    5 - call back the input image with MPR:orig and +swap to get the composited at the top

    6 - Again -composite... this time using CopyOpacity method. This one will replace any existing alpha channel of the image with the greyscale image given (here it is the image obtained but the first -composite operation)

    This should be improvable as with this command, magick is issuing a warning with RGB images due to the layer in Gray colorspace