如何将函数应用于 numpy 数组中的每个第三轴元素?
如果我有一个像这样的 numpy 数组:
[[[137 153 135]
[138 154 136]
[138 153 138]
...,
[134 159 153]
[136 159 153]
[135 158 152]]
...,
[ 57 44 34]
[ 55 47 37]
[ 55 47 37]]]
如何将函数应用于每个 [000 000 000] 条目并修改它?
# a = numpy array
for x in a:
for y in x:
y = modify(y)
我想要实现的是修改已转换为 numpy 数组的 PIL 图像中的每个(r,g,b)像素。
If I have a numpy array like so:
[[[137 153 135]
[138 154 136]
[138 153 138]
...,
[134 159 153]
[136 159 153]
[135 158 152]]
...,
[ 57 44 34]
[ 55 47 37]
[ 55 47 37]]]
How can I apply a function to each [000 000 000] entry, modifying it?
# a = numpy array
for x in a:
for y in x:
y = modify(y)
What I'd like to achieve is modifying each (r,g,b) pixel in a PIL image that was converted to a numpy array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对你的问题的一个简单回答是
,但这不会很有效。有效的解决方案应该避免对所有像素进行 Python 循环。 (这就是 NumPy 的全部内容——矢量化你的代码!)手头的情况的矢量化版本将是
A simple answer to your question is
This won't be very efficient, though. An efficient solution should avoid Python loops over all pixels. (That's somehow what NumPy is all about -- vectorise your code!) A vectorised version for the case at hand would be
y
有你的 RGB 数组,不是吗?或者更一般地说:
y
there is your rgb array, isn't it?or more generally: