make parts of edges soft with ImageMagick

84 Views Asked by At

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.

2

There are 2 best solutions below

0
user1850133 On BEST ANSWER
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

2
GeeMack On

In a GIF image, the transparency of a pixel can be fully on or fully off, only 100% transparent or 100% opaque. To make an image with gradient transparencies you need to use a format that supports it, PNG probably being the most common.

To create that result as a PNG image with ImageMagick you can create your base image, separate it into the red, green, blue , and alpha channels, create an aplha channel that includes that gradient blur, and re-combine the channels for the output.

This command is in Windows syntax. (A version for *nix OS is below.)

magick -size 120x120 -background none xc:none -alpha set -stroke none ^
   -draw "fill SpringGreen2 rectangle 0,100 120,120" ^
   -draw "fill SpringGreen3 roundrectangle 20,60 100,120 10,10" ^
   -draw "fill SpringGreen circle 60,40 60,65" ^
   -channel RGBA -separate ^
   (  xc:white -bordercolor black -shave 6x6 -border 6x6 -blur 0x6 ^
      -clone -1 -compose darken -composite -compose over ) ^
   -delete -2 -combine result.png

That command produces this image...

enter image description here

Inside the parentheses the command creates a blurred-edge image and composites that with the separated alpha channel. After the parentheses it combines the R, G, B, and that newly created alpha channel to produce the output result.

Another contributor tested this, and it should work on a *nix OS...

magick -size 120x120 -background none xc:none -alpha set -stroke none \
   -draw 'fill SpringGreen2 rectangle 0,100 120,120' \
   -draw 'fill SpringGreen3 roundrectangle 20,60 100,120 10,10' \
   -draw 'fill SpringGreen circle 60,40 60,65' \
   -channel RGBA -separate \
   \(  xc:white -bordercolor black -shave 6x6 -border 6x6 -blur 0x6 \
      -clone -1 -compose darken -composite -compose over \) \
   -delete -2 -combine result.png