Question

I've a number of images with transparent pixels both inside the graphics and outside. One example is: enter image description here

and

enter image description here

So now I want to fill only the internal transparent pixels (the ones within the black boarder), is there any batch processing way to do it? I tried imagemagick convert tool, but didn't figure out how to the the "selective" conversion.

Thanks for any help!

Was it helpful?

Solution

there are following ways how can accomplish that (it all depends on how the image is composed):

if you know the part you want to keep transparents starts at 0x0 You start by removing all transparency with something like http://www.imagemagick.org/Usage/masking/#remove

and then re-add the transparency using flood-fill starting at 0x0 http://www.imagemagick.org/Usage/masking/#bg_remove

You have more control over whats going on by using a transparency mask:

then you start modifying the mask (which is now only black/white), by coloringusing floddfill starting wiht 0x0 (i.e. with blue), replacing the black with white and then replacing all blue pixel with black (for color replacement see http://www.imagemagick.org/Usage/color_basics/#replace)

# the mask
convert original.png -alpha extract  mask.png
convert mask.png -fill blue -draw 'color 0,0 floodfill' mask_blue.png
convert mask_blue.png -fill white -opaque black mask_filled_blue.png
convert mask_filled_blue.png -fill black -opaque blue mask_filled.png
# change transparent to skyblue
convert original.png -background skyblue -alpha remove -alpha off original_nontransparent.png
# apply the modified mask
convert original_nontransparent.png mask_filled.png -alpha Off -compose CopyOpacity -composite final.png

Note that this techniqe works only moderatly well with half transparent things (see http://www.imagemagick.org/Usage/antialiasing/#floodfill ).

For better results with half-transparency you might want to use different methods to "fill" the mask. You would i.e. just draw a shape instead of the two fill operations on the mask:

  convert mask.png -fill black -draw "circle 40,80 60,60" mask_filled.png

this will fill the center, but keep the half transparency intact.

if you know the "center" is always trnsparent, you could also floodfill from the center.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top