Analyze image and fill match with neighboring color
Posted: 2008-04-28T10:47:58-07:00
Looking for some ideas for the following:
Analyze image pixel by pixel looking for white. If white pixel found, fill with neighboring color.
I wrote a script that parses the text output from convert and accomplishes what I outlined above. However, as you might imagine, it is very slow to analyze a large image.
My next attempt at this will be to look at the API to hopefully gain speed and/or write the script to keep track of where the last fill happened start parsing there...
But I was wondering if there would be a way to accomplish the same with convert's FX or other method?
Here's the Perl code that I'm using, approximately:
Example image:
NOTE: the example image has a white background. I want to look at just the white in the object so I fill at pixel 1,1 with grey or some other color and then change it back to white after the loop.
Analyze image pixel by pixel looking for white. If white pixel found, fill with neighboring color.
I wrote a script that parses the text output from convert and accomplishes what I outlined above. However, as you might imagine, it is very slow to analyze a large image.
My next attempt at this will be to look at the API to hopefully gain speed and/or write the script to keep track of where the last fill happened start parsing there...
But I was wondering if there would be a way to accomplish the same with convert's FX or other method?
Here's the Perl code that I'm using, approximately:
Code: Select all
&fill;
sub fill {
open(EXE,"convert pre.png txt:- |");
while(<EXE>) {
chomp;
# 11,0: (48830,48830,48830, 0) #BEBEBEBEBEBE grey
$_ =~ /(.*?):/;
$coords = $1;
$_ =~ /\#(.*?)\s+(.*)/;
$color = $2;
$color =~ s/\s+//g;
if ($color =~ /white/i) {
`convert -fuzz 10% -fill '$last_color' -draw 'color $coords floodfill' pre.png tmp.png`;
`mv tmp.png pre.png`;
close(EXE);
&fill;
}
else {
$last_color = $color;
}
}
}
NOTE: the example image has a white background. I want to look at just the white in the object so I fill at pixel 1,1 with grey or some other color and then change it back to white after the loop.