How to flood-fill part of a bitmap enclosed by a black border with my chosen color?

这一生的挚爱 提交于 2019-12-28 06:26:50

问题


I want to programmatically modify a bitmap using python but don't really need a thorough grounding in the subject, so would like to concentrate on learning just what I need to get the job done.

A good example of the kind of thing I'm after would be a bitmap image of england and it's counties. This would initially display a black border around all the counties on a white background.

So far so good, but how can I dynamically change the background color of a county?

Off the top of my head I was thinking I might find a flood-fill routine that works similar to a simple paint app. Something that changes all the pixels within an area enclosed by a specified color. I've had a quick look at the PIL documentation but didn't find anything I recognised as a flood fill function?

I don't yet know exactly what a mask is or how to use it but maybe this is an avenue I should explore. Maybe I could define a mask for each county and then use the mask to guide the fill process? Can masks be defined and stored within the bitmap for later use by my program?

Same goes for paths???

Any help or pointers would be greatly appreciated.


回答1:


PIL has an undocumented function ImageDraw.floodfill:

>>> import ImageDraw
>>> help(ImageDraw.floodfill)
Help on function floodfill in module ImageDraw:

floodfill(image, xy, value, border=None)
    Fill bounded region.

(Flood-filling should generally be a last resort because it interacts poorly with anti-aliased lines. It is usually better to get the actual boundary data for the counties and then draw a filled polygon. However, PIL doesn't support anti-aliased line drawing so this advice is useless unless you switch your drawing module to something more capable like PythonMagick or pycairo.)




回答2:


You can try the opencv binding in python. Here is some example: http://opencv.willowgarage.com/documentation/python-introduction.html

You can then use the cvFloodFill function to flood fill a region.



来源:https://stackoverflow.com/questions/10026346/how-to-flood-fill-part-of-a-bitmap-enclosed-by-a-black-border-with-my-chosen-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!