title: cv2.imread: checking if image is being read
body: <p>I'm writing an OpenCV program in python, and at some point I have something like </p>
  
  <pre><code>import cv2
  import numpy as np
  ... 
  img = cv2.imread("myImage.jpg")
  
  # do stuff with image here 
  </code></pre>
  
  <p>The problem is that I have to detect if the image file is being correctly read before continuing. <code>cv2.imread</code> returns <code>False</code> if not able to open the image, so I think of doing something like:</p>
  
  <pre><code>if (img):
     #continue doing stuff
  </code></pre>
  
  <p>What happens is that if the image is not opened (e.g. if the file does not exist) <code>img</code> is equal to <code>None</code> (as expected). However, when <code>imread</code> works, the condition, breaks:</p>
  
  <pre><code>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  </code></pre>
  
  <p>i.e. the returned <code>numpy.ndarray</code> cannot be used as a boolean. The problem seems to be that <code>imread</code> returns numpy.ndarray if success and <code>False</code> (boolean) otherwise. </p>
  
  <p>My solution so far involves using the <code>type</code> of the returned value as follows:</p>
  
  <pre><code>if (type(img) is np.ndarray): 
       #do stuff with image
  </code></pre>
  
  <p>But I was wondering: isn't there a nicer solution, closer to the initial check <code>if(img): #do stuff</code> ? </p>