-3

i want to extract 2 photo present in my directory using a for loop . in each iteration it will bring a photo. After getting both photos another if statement will trigger and will print "Both images extracted from directory successfully". This is the code ==>

import cv2

import os

import re

from skimage.io import imread,imshow,imsave

images =os.listdir('D:\programs python/regeneration\Mi3_Aligned/1')

img = None

ref_img = None

for i in images:

    if i == "1.bmp":

        img = imread('D:\programs python/regeneration\Mi3_Aligned/1/' + i)

        img = cv2.resize(img, (980, 980), cv2.INTER_AREA)

    if i == "2.bmp":

        ref_img = imread('D:\programs python/regeneration\Mi3_Aligned/1/'+ i)

        ref_img = cv2.resize(img, (980, 980), cv2.INTER_AREA)

    if (img!=None and ref_img!=None):

        print("Both images extracted from directory successfully")

But it is generating an error and i cannot understand what is the problem

if (img!=None and ref_img!=None):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please Help!!!

3 Answers3

2

You can fix this by changing

if (img!=None and ref_img!=None):

to

if img is not None and ref_img is not None:

Arrays have an implementation of == and != that returns another array instead of a single boolean value. But ... is not None will always evaluate to true or false.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • the crucial point is that `==` and `!=` do a "value comparison". numpy arrays implement this behavior. an `is` or `is not` comparison is an "identity comparison". it checks whether the *objects* are the *same* (as `None` in this case), without looking at the values in those objects. – Christoph Rackwitz Jul 04 '21 at 12:37
-1

The problem stems from the comparison between an array and a different type of variable / value. I suggest using this instead:

if img and ref_img:
    print("Both images extracted from directory successfully")

If you omit the variable to compare to, python just checks that the values are not empty / None.

A.M. Ducu
  • 892
  • 7
  • 19
  • won't work. if both are np.arrays, `and` between them will only throw the same error as in the question, because np.arrays can't be trivially converted to booleans. `img is not None and ref_img is not None` required here and the issue was using `!=` instead of `is`/`is not`, so yes this stems from value-comparing an np.array to None... but no, identity-compare should have been use. – Christoph Rackwitz Jul 04 '21 at 00:14
-1

The error is giving you a hint, you're trying to compare a variable that's an array.

The docs for imread() say that it returns an array.

Rather than comparing against None you can take advantage of the fact that an empty array in Python is falsey.

if img and ref_img:
    print('Both images extracted from directory successfully')
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • you can't do `bool(some_nparray)`. for actual arrays (None isn't an array), it'll refuse and throw the error as in the question. Python has no `&&` operator. even `img and ref_img` will not work because it's just giving the same error as in the question. `img & ref_img` will also not work because that merely tries an element-wise *bitwise and*, which results in an array, which doesn't "decay" to true/false on its own – Christoph Rackwitz Jul 03 '21 at 23:08
  • @ChristophRackwitz I meant `and`. I forgot that it's Ruby that prefers `&&` over `and`, not python (fixed). I just tried a REPL with two empty arrays and they definitely came up falsey. The library in question returns normal arrays, not numpy arrays, according to the docs, so this solution would work fine. – Soviut Jul 04 '21 at 03:25
  • 1
    `cv2.resize` *will* return a numpy array, and that's what is in those variables at the time they're checked. the error message from the question would not have happened if these objects were regular python lists. python lists don't have `.any` or `.all` methods. that absolutely indicates numpy. even the scipy.io.imread call will return an `ndarray`. how can you claim that regular python lists were involved? – Christoph Rackwitz Jul 04 '21 at 12:30
  • @ChristophRackwitz you really should have made your own answer since you clearly have more insight. An explanation of numpy arrays and the fact that cv2.resize returns them should be in the accepted answer. – Soviut Jul 04 '21 at 19:04