title: Read a base 64 encoded image from memory using OpenCv python library
body: <p>I'm working on an app that to do some facial recognition from a webcam stream. I get base64 encoded data uri's of the canvas and want to use it to do something like this:</p>
  
  <pre><code>cv2.imshow('image',img)
  </code></pre>
  
  <p>The data URI looks something like this:</p>
  
  <pre><code> data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7
  </code></pre>
  
  <p>So, for clarity I've shown what the image looks like so the base64 string is not broken.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>The <a href="http://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html" rel="noreferrer">official doc</a> says, that <code>imread</code> accepts a file path as the argument. From <a href="https://stackoverflow.com/a/16214280/1083314">this</a> SO answer, if I do something like:</p>
  
  <pre><code> import base64
   imgdata = base64.b64decode(imgstring) #I use imgdata as this variable itself in references below
   filename = 'some_image.jpg'
   with open(filename, 'wb') as f:
      f.write(imgdata)
  </code></pre>
  
  <p>The above code snippet works and the image file gets generated properly. However I don't think so many File IO operations are feasible considering I'd be doing this for every frame of the stream. I want to be able to read the image into the memory directly creating the <code>img</code> object.</p>
  
  <p>I have tried two solutions that seem to be working for some people.</p>
  
  <ol>
  <li><p>Using PIL <a href="https://stackoverflow.com/a/11601466/1083314">reference</a>:</p>
  
  <pre><code>pilImage = Image.open(StringIO(imgdata))
  npImage = np.array(pilImage)
  matImage = cv.fromarray(npImage)
  </code></pre>
  
  <p>I get <code>cv</code> not defined as I have openCV3 installed which is available to me as <code>cv2</code> module. I tried <code>img = cv2.imdecode(npImage,0)</code>, this returns nothing.</p></li>
  <li><p>Getting the bytes from decoded string and converting it into an numpy array of sorts</p>
  
  <pre><code>file_bytes = numpy.asarray(bytearray(imgdata), dtype=numpy.uint8)
  img = cv2.imdecode(file_bytes, 0) #Here as well I get returned nothing
  </code></pre></li>
  </ol>
  
  <p>The documentation doesn't really mention what the <code>imdecode</code> function returns. However, from the errors that I encountered, I guess it is expecting a <code>numpy array</code> or a <code>scalar</code> as the first argument. How do I get a handle on that image in memory so that I can do <code>cv2.imshow('image',img)</code> and all kinds of cool stuff thereafter.</p>
  
  <p>I hope I was able to make myself clear.</p>