如何使用我选择的颜色填充黑色边框包围的位图部分? [英] How to flood-fill part of a bitmap enclosed by a black border with my chosen color?

查看:18
本文介绍了如何使用我选择的颜色填充黑色边框包围的位图部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 python 以编程方式修改位图,但实际上并不需要对该主题有深入的了解,因此我想专注于学习完成工作所需的知识.

我所追求的那种东西的一个很好的例子是英格兰及其县的位图图像.这最初会在白色背景上的所有县周围显示黑色边框.

到目前为止一切都很好,但是如何动态更改县的背景颜色?

在我的脑海里,我想我可能会找到一个类似于简单的绘画应用程序的洪水填充程序.改变由指定颜色包围的区域内的所有像素的东西.我快速浏览了 PIL 文档,但没有找到任何我认为是洪水填充函数的内容?

我还不知道面具是什么或如何使用它,但也许这是我应该探索的途径.也许我可以为每个县定义一个掩码,然后使用掩码来指导填充过程?可以在位图中定义和存储掩码以供我的程序以后使用吗?

路径也一样???

任何帮助或指示将不胜感激.

解决方案

PIL 有一个未公开的函数 ImageDraw.floodfill:

<预><代码>>>>导入 ImageDraw>>>帮助(ImageDraw.floodfill)ImageDraw 模块中函数 floodfill 的帮助:洪水填充(图像,xy,值,边框=无)填充有界区域.

(洪水填充通常应该是最后的手段,因为它与抗锯齿线的交互作用很差.通常最好先获取县的实际边界数据,然后绘制填充的多边形.但是,PIL 不支持抗锯齿线条绘制所以这个建议是没有用的,除非你将你的绘图模块切换到更强大的东西,比如 PythonMagickpycairo.)

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.

解决方案

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.)

这篇关于如何使用我选择的颜色填充黑色边框包围的位图部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆