ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()python [英] ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() python

查看:143
本文介绍了ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求使用带有np.meshgrid和np.angle()的网格(100,100)来制作下面的图像,而我唯一的问题是当我想制作最终的布尔网格时,python说:ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all().

I was asked to make the figure of the image bellow, using a grid (100,100) with the np.meshgrid and np.angle() and my only problem is that when i want to make the final boolean grid, python says: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

from PIL import Image
import numpy as np

XX = np.arange(0,101)
YY = np.arange(0,101) 
x, y = np.meshgrid(XX, YY)

x = x-50
y = y-50

z = x+ y*-1j

print z    

a = np.angle(z,deg=True)

grid = (a>=0 and a<20) or (a>=40 and a<60) or (a>=80 and a<100) or (a>=120 and a<140) or++ (a<=-20 and a>-40) or (a<=-60 and a>-80) or (a<=-100 and a>-120) or (a<=-140 and a>-160)


grid = grid.astype('uint8') * 255
new_img = Image.fromarray(grid ,'L') 
new_img.save("grid .bmp")

推荐答案

分别对andor尝试&|*+.

Try & and | or * and +, respectively for and and or.

"and"和"or"比较整个对象的整体真实性,但是您需要元素级和的类似物.还有一些numpy函数logical_andlogical_or更明确.

"and" and "or" compares the whole truthiness of the whole object, but you want the analogue of element-wise and. There is are also numpy functions logical_and and logical_or to be more explicit.

这篇关于ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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