Page 1 of 1

How to mask a source Png Image ?

Posted: 2018-02-09T02:57:42-07:00
by Alexvb6
Hi,

Using IM 6.9 on Windows, I am stuck for hours on a very simple case : masking a source Png image with a Black and white mask !
I am stuck on the 4th step, when composing.
  • Step 1. I have a colorful source image in Png.
  • Step 2. I Crop a portion, apply a paint effect to it, then add a 4px border around it : IT WORKS.
  • Step 3. I create a rounded mask of the size of this image : IT WORKS.
  • Step 4. I apply the mask to the image, in order to remove its corners : IT DOES NOT WORK.
I have tried a lot of -compose methods, without any success, even by reading again and again the http://www.imagemagick.org/Usage/compose/#copyopacity page.

Could you point me to my error ?

Thank you very much

Here is my code (divided into several lines, and commented, just to make it clear for you).
(I obviously use it into a single line !)

Code: Select all

#STEP 1 : The source file :
Image

Code: Select all

#STEP 2 : crop, paint, then add the lightblue border the source transparent Png : 4px rounded rectangle : WORKS
convert MY_FILE_IN.PNG -crop 690x690+555+555 +repage -paint 4 -stroke #e2efff -strokewidth 4 -fill transparent -draw "roundrectangle 2,2 689,689 5,5" -write mpr:DstImageBordered +delete
Image

Code: Select all

#STEP 3 : create a mask : a rounded rectangle : WORKS
convert -size 692x692 xc:black -fill white -draw "roundrectangle 0,0 691,691 5,5" -write mpr:DstImageCutMask +delete
Image

Code: Select all

#STEP 4 : mask the source image with the firstly created mask : DOES NOT WORK
mpr:DstImageCutMask mpr:DstImageBordered -alpha Off -compose CopyOpacity -composite MY_FILE_OUT.PNG
Here is the problem ! I am not able to mask correctly...such a shame !

Re: How to mask a source Png Image ?

Posted: 2018-02-09T03:03:55-07:00
by snibgo
"mpr:" is a format for storing images in memory, only for the duration of the command ("convert" or "magick"). When the command ends, any images in "mpr:" will vanish.

If you don't save images to disk during or at the end of a command, they will not be available after the command ends.

You can do what you want in a single command.

Re: How to mask a source Png Image ?

Posted: 2018-02-09T03:56:39-07:00
by Alexvb6
Hi Snigbo, Obviously I know how MPR works ;-)
I have precised in my quesiton that I have splitted my command-line for ease of rading purposes.
Here is the complete command line I use. The composition does not work, that is the sole problem :

Code: Select all

convert MY_FILE_IN.PNG -crop 690x690+555+555 +repage -paint 4 -stroke #e2efff -strokewidth 4 -fill transparent -draw "roundrectangle 2,2 689,689 5,5" -write mpr:DstImageBordered +delete     -size 692x692 xc:black -fill white -draw "roundrectangle 0,0 691,691 5,5" -write mpr:DstImageCutMask +delete    mpr:DstImageCutMask mpr:DstImageBordered -alpha Off -compose CopyOpacity -composite MY_FILE_OUT.PNG

Re: How to mask a source Png Image ?

Posted: 2018-02-09T05:14:02-07:00
by snibgo
I find commands easier to read when they are split into lines, like this:

Code: Select all

convert ^
  MY_SOURCE_FILE.PNG ^
  -crop 690x690+555+555 +repage ^
  -paint 4 ^
  -stroke #e2efff -strokewidth 4 ^
  -fill transparent -draw "roundrectangle 2,2 689,689 50,50" ^
+write x0.png ^
  -write mpr:DstImageBordered +delete ^
  -size 692x692 xc:black -fill white ^
  -stroke none -strokewidth 0 ^
  -draw "roundrectangle 0,0 691,691 50,50" ^
+write x1.png ^
  -write mpr:DstImageCutMask +delete ^
  mpr:DstImageBordered ^
  mpr:DstImageCutMask ^
  -alpha Off ^
  -compose CopyOpacity -composite ^
  MY_FILE_OUT.PNG
I inserted "+write x0.png" etc so I could see the intermediate results. This showed your second image was entirely white.

I changed your radii to "50" to make them easier to see. You second image uses a stroke, colour #e2efff, width 4. I guessed you didn't want that, so I set it to no stroke.

You copied the opacity from "Bordered" to "Cut Mask". I guessed you wanted it the other way round, so I reversed them.

Re: How to mask a source Png Image ?

Posted: 2018-02-09T17:21:45-07:00
by Alexvb6
Hi Snigbo,
  1. I was not aware that my second image was inheriting the stroke parameters from the first. Very interesting thing !
    So if I understand well, after having specified "stroke colour #e2efff width 4" for my first image, the second image was inheriting and using these parameters also, even after using "-write +delete" ?
    That's interesting, because I was (falsely) believing that deleting the temporary image was also resetting the parameters... but considering it another way, that sounds useful to keep the parameters between several "-write +delete", for reusing them. Am I correct on this assumption ? So How could I erase all parameters instead of resetting them like you did with "-stroke none -strokewidth 0" ?
    `
  2. I think I had already tried to reverse the "Bordered" and "Cut Mask" images like you did, but maybe I had tried this when trying another compose mode than "- compose CopyOpacity" in the same time (I should never change TWO things at a time, I know !).
    `
  3. So the conclusion is that your suggestions and rectifications to my code made it WORK correctly, and as expected, except a last problem (detailed in the last point) !
    I must apologize for not having properly formatted the script code in my initial question : your answer is far more comprehensible and easier to read. I will remember of this great code formatting for the next time I will need to exchange on the forum ! To be honest with you, my code is used in the VBScript COM+ interface, so I have rewritten it in command-line for my question.. maybe too quickly :-)
`

[*] The last problem is that the generated image have its initial alpha channel removed. My initial SOURCE image is semi-transparent (50%), and My final image is 100% opaque.. How could I fix that ?

I thank you very much Snigbo :
You are pretty much active on this forum, and I understand why ! :)

Re: How to mask a source Png Image ?

Posted: 2018-02-09T18:01:08-07:00
by Alexvb6
Hi again,

Hi have found by myself how to preserve the initial transparency of the original source image : It is needed to extract its initial alpha channel, then combine it with the rounded mask, using the -compose Multiply operator. Here is the complete working code, based on Snigbo's one and my modifications preserving the Alpha channel of the original Source image :

Code: Select all

convert ^
  MY_SOURCE_FILE.PNG ^
  -crop 690x690+555+555 +repage ^
  -paint 4 ^
  -stroke #e2efff -strokewidth 4 ^
  -fill transparent -draw "roundrectangle 2,2 689,689 50,50" ^
  +write x0.png ^
  -write mpr:DstImageBordered +delete ^

  -size 692x692 xc:black -fill white ^
  -stroke none -strokewidth 0 ^
  -draw "roundrectangle 0,0 691,691 50,50" ^
  +write x1.png ^
  -write mpr:DstImageCutMask +delete ^

  MY_SOURCE_FILE.PNG ^
  -set colorspace RGB -alpha extract -write mpr:ExtractedInitialImageAlpha ^
  +delete ^

  mpr:ExtractedInitialImageAlpha mpr:DstImageCutMask -alpha Off -compose Multiply -composite ^
  -write mpr:SrcImageBothMasks ^
  +delete ^

  mpr:DstImageBordered ^
  mpr:SrcImageBothMasks ^
  -alpha Off ^
  -compose CopyOpacity -composite ^
  MY_FILE_OUT.PNG

Re: How to mask a source Png Image ?

Posted: 2018-02-09T18:37:00-07:00
by snibgo
I included "+write x0.png" and "+write x1.png" to save the intermediate results, so we could see them. You can remove them when the command does what you want. "+write" is a really useful debugging tool.

1. "-stroke" and "-strokewidth" are both settings. Settings change the way that operators (such as "-draw") work. Within a command, each setting keeps its value until changed.

(However, "-respect-parentheses" mean that settings made inside a parenthesis are restored at the close-parenthesis, and in IM v7 curly brackets "{}" does something similar. I don't use either of these.)

Settings have to be changed individually. There is no "-reset-all-settings" command.

2. Most "-compose XX -composite" operations change the first image, according to something in the second image.

3. Good stuff. We each have different styles for laying out commands. My web pages have many examples of Windows BAT commands, and I generally have short lines because the outputs are shown beside them.

4. Yes, that's it, multiply the two alphas together.