Cutting part from image with a mask

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
defied

Cutting part from image with a mask

Post by defied »

Hello,

I've been trying to get this to work all afternoon, I thought it would be simple but I can't get it to work :(
I have a .png mask, the black part of the mask indicates the area I want from a transparent .gif

First I make a tile of images and save it in a .gif with transparency:

Code: Select all

convert -background None -size 373x280 tile:heart.png tile.gif
Image

Then I have this mask:
Image

The black area needs to be filled with the heart tiles, while the rest needs to remain transparent.
But this is what happens:

Code: Select all

convert tile.gif mask.png -compose CopyOpacity -composite -matte output.gif
Image

So it's almost good, but the black color needs to be transparent. I know I can remove it with -transparent but if the tile.gif happens to have black in it it would remove too much. Can someone help me extract a part from a transparent gif and save it in a transparent png?
defied

Re: Cutting part from image with a mask

Post by defied »

Ok well, I found the solution:

Code: Select all

composite -compose Dst_In mask.png tile.gif -matte output.gif
defied

Re: Cutting part from image with a mask

Post by defied »

Does someone know how I can make a mask look cleaner?

Right now I have this:

Image

but the little white dots in the right lower corner should be replaced with black, that way I can get a clean mask without any holes or noise.

Thanks
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Cutting part from image with a mask

Post by Bonzo »

How are you creating your mask ?

If you are using a colour would adding a -fuzz help?
defied

Re: Cutting part from image with a mask

Post by defied »

the mask is a transparent png which I receive by flash's bitmapdata.. I tried various stuff but did not come up with a solution.
I'll keep looking and post here if I find a solution.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cutting part from image with a mask

Post by anthony »

To clean your mask, first -draw fill the outside area with some color.

now overlay the mask on black to fill in the 'holes'

You can now replace the original fill color with thransparency again.

To smooth the result blur it with -channel RGBA setting, and threshold the result.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply