title: PIL: Convert Bytearray to Image
body: <p>I am trying to verify a bytearray with <code>Image.open</code> and <code>Image.verify()</code> without writing it to disk first and then open it with <code>im = Image.open()</code>. I looked at the <code>.readfrombuffer()</code> and <code>.readfromstring()</code> method, but there I need the size of the image (which I could only get when converting the bytestream to an image).</p>
  
  <p>My Read-Function looks like this:</p>
  
  <pre><code>def readimage(path):
      bytes = bytearray()
      count = os.stat(path).st_size / 2
      with open(path, "rb") as f:
          print "file opened"
          bytes = array('h')
          bytes.fromfile(f, count)
      return bytes
  </code></pre>
  
  <p>Then as a basic test I try to convert the bytearray to an image:</p>
  
  <pre><code>bytes = readimage(path+extension)
  im = Image.open(StringIO(bytes))
  im.save(savepath)
  </code></pre>
  
  <p>If someone knows what I am doing wrong or if there is a more elegant way to convert those bytes into an image that'd really help me.</p>
  
  <p>P.S.: I thought I need the bytearray because I do manipulations on the bytes (glitch them images). This did work, but I wanted to do it without writing it to disk and then opening the imagefile from the disk again to check if it is broken or not.</p>
  
  <p>Edit: All it gives me is a <code>IOError: cannot identify image file</code></p>