title: PIL Image as Bytes with BytesIO to prevent hard disk saving
body: <p><strong>Problematic</strong></p>
  <p>I have a PIL Image and i want to convert it to a bytes array. I can't save the image on my hard disk so i can't use the default <code>open(file_path, 'rb')</code> function.</p>
  <p><strong>What i tried</strong></p>
  <p>To overturn this problem i'm trying to use the <code>io</code> library doing this :</p>
  <pre><code>buf = io.BytesIO()
  image.save(buf, format='JPEG')
  b_image = buf.getvalue()
  </code></pre>
  <p>Considering image as a functional PIL Image.</p>
  <p>the &quot;b_image&quot; will be used as argument for the Microsoft Azure cognitives services function <code>read_in_stream()</code></p>
  <p>If we look in the documentation, we can see that this function image argument have to be :</p>
  <blockquote>
  <p>image
  xref:Generator</p>
  </blockquote>
  <blockquote>
  <p>Required</p>
  </blockquote>
  <blockquote>
  <p>An image stream.</p>
  </blockquote>
  <p>Documentation available <a href="https://docs.microsoft.com/fr-fr/python/api/azure-cognitiveservices-vision-computervision/azure.cognitiveservices.vision.computervision.operations.computervisionclientoperationsmixin?view=azure-python#read-in-stream-image--language--en---custom-headers-none--raw-false--callback-none----operation-config-" rel="nofollow noreferrer">here</a></p>
  <p><strong>The issue</strong></p>
  <p>When i execute it i got the error :</p>
  <blockquote>
  <p>File &quot;C:...\envs\trainer\lib\site-packages\msrest\service_client.py&quot;, line 137, in stream_upload
  chunk = data.read(self.config.connection.data_block_size)</p>
  </blockquote>
  <blockquote>
  <p>AttributeError: 'bytes' object has no attribute 'read'</p>
  </blockquote>
  <p>There is no error in the client authentification or at another point because when i give as parameter an image imported with this line :</p>
  <p><code>image = open(&quot;./1.jpg&quot;, 'rb')</code></p>
  <p>Everything is working correctly..</p>
  <p><strong>Sources</strong></p>
  <p>I also saw <a href="https://stackoverflow.com/questions/33101935/convert-pil-image-to-byte-array/33117447">this</a> post that explains exactly what i want to do but in my case it's not working. Any idea would be appreciated.</p>