title: PIL with BytesIO: cannot identify image file
body: <p>I am trying to send an image over socket connection for video chat, but the reconstruction of the image from bytes format is incorrect. Here is my conversion of the image to bytes to send: </p>
  
  <pre><code>pil_im = Image.fromarray(img)
  b = io.BytesIO()
  pil_im.save(b, 'jpeg')
  im_bytes = b.getvalue()
  return im_bytes
  </code></pre>
  
  <p>This sends fine, however, I cannot resolve the reformatting of these bytes into an image file. Here is my code to reformat into image for display:</p>
  
  <pre><code>pil_bytes = io.BytesIO(im_bytes)
  pil_image = Image.open(pil_bytes)
  cv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
  return cv_image
  </code></pre>
  
  <p>The second line there raises the following exception:</p>
  
  <blockquote>
    <p>cannot identify image file &lt;_io.BytesIO object at 0x0388EF60></p>
  </blockquote>
  
  <p>I have looked at some other threads (<a href="https://stackoverflow.com/questions/23587426/pil-open-method-not-working-with-bytesio">this one</a> and <a href="https://stackoverflow.com/questions/31077366/pil-cannot-identify-image-file-for-io-bytesio-object?lq=1">this one</a>) but no solution has been helpful for me. I am also using <a href="https://github.com/anilshanbhag/videochat/blob/master/videofeed.py" rel="nofollow noreferrer">this</a> as reference to try to correct myself, but what seems to work fine for them just doesn't for me.
  Thank you for any assistance you can provide and please excuse any errors, I am still learning python.</p>