在 pygame 表面中将白色像素的随机子集设为黑色

发布于 2024-11-08 08:03:11 字数 187 浏览 0 评论 0原文

我有一个 pygame 表面(一些由 font.render 创建的文本),除了一些像素是白色之外,大部分是透明的。我想将这些白色像素的一半改为黑色。我知道我可以迭代每个像素,通过 get_at() 检查它是否是白色并使用 set_at() 更改其颜色,但我突然想到必须有一种更快的方法来告诉 pygame 获取所有白色像素并更改随机一半为黑色。有什么想法吗?

I have a pygame surface (some text created by font.render) that is mostly transparent except some pixels are white. I'd like to change half of these white pixels black. I know I could iterate through each pixel, checking whether it is white by get_at() and changing its color using set_at(), but it strikes me that there must be a faster way of telling pygame to grab all the white pixels and change a random half of them to black. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

但可醉心 2024-11-15 08:03:11

我认为您需要遍历所有像素来检查它们是否是白色的。获得白人列表后,请执行以下操作:

for px in random.sample(whites,len(whites)/2):
    Surface.set_at((px.x,px.y),'black')

Python 文档 - 随机

I think you'll need to iterate through all the pixels to check if they're white. Once you have the list of whites, do something like:

for px in random.sample(whites,len(whites)/2):
    Surface.set_at((px.x,px.y),'black')

Python Documentation - random

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文