# Code for a color bucket fill (seed fill) of a bounded irregular area?

In images like the one shown here, I would like to be able to pick an x,y coordinate and fill the bounded interior with a specified color. That functionality is well known in PhotoShop and GIMP, but I can't seem to find even a simple version of such code for Mathematica. I'm not expecting a great GUI - just some code that I can use and modify.

For[i = 1, i <= 100, i++,
t[i] = RandomReal[{0, 1}, {20, 2}]];

For[i = 1, i <= 100, i++,
g[i] = Graphics[{AbsoluteThickness[1],
BezierCurve[t[i], SplineClosed -> True,
SplineDegree -> RandomInteger[{4, 16}]]}, PlotRange -> {0, 1},
ImageSize -> {1000, 1000}]; Print[i];
Print[g[i]]]

• Is the input a Graphics or an Image object?
– kglr
Mar 6, 2021 at 17:13
• @kglr Graphics. I added some code to the figure. Watch out for the 100 iterations. Mar 6, 2021 at 18:55
• After getting the jaggies with Rasterize, I tried this and it works nicely: labels = MorphologicalComponents[g[1]] Colorize[labels]  Mar 6, 2021 at 20:55
• A related question: (41118) Apr 7, 2021 at 17:15

You can remove border components from MorphologicalComponents using DeleteBorderComponents or SelectComponents, colorize and create a mesh object using ImageMesh:

imgMesh = ImageMesh @ Colorize @ DeleteBorderComponents @
MorphologicalComponents @ Rasterize[#, ImageResolution -> 200] &


Using a random sample of size 9 from OP's g /@ Range[100]:

SeedRandom[1]
Multicolumn[Graphics[{RandomColor[], EdgeForm[Gray], #} & /@
MeshPrimitives[imgMesh @ g @ #, 2], ImageSize -> 250] & /@
RandomSample[Range[100], 9], 3]


We get the same picture using imgMesh2 where

imgMesh2 = ImageMesh @ Colorize @
SelectComponents[
MorphologicalComponents @ Rasterize[#, ImageResolution -> 200],