title: a bytes-like object is required, not 'Image'
body: <p>I want to write an Image object to disk but I keep getting the error:</p>
  
  <pre><code> a bytes-like object is required, not 'Image'
  </code></pre>
  
  <p>First I convert a String to an array and then I create an Image using that array.</p>
  
  <pre><code>    class Item(object):
  
  def __init__(self, patch, coords, label):
  
      self.channels = patch.shape[2]
      # Assuming only square images.
      self.size = patch.shape[0]
      self.data = patch.tobytes()
      self.label = label # Integer label ie, 2 = Carcinoma in situ
      self.coords = coords
  
  def get_label_array(self, num_classes):
      l = np.zeros((num_classes))
      l[self.label] = 1
      return l
  
  def get_patch(self):
      return np.fromstring(self.data, dtype=np.uint8).reshape(self.size, self.size, self.channels)
  
  def get_patch_as_image(self):
      return Image.fromarray(self.get_patch(), 'RGB')
  </code></pre>
  
  <p>Is there any way that I can save the image using:</p>
  
  <pre><code>def save_in_disk(patches, coords, file_name, labels=[]):
      use_label = False
      if len(labels) &gt; 0:
          use_label = True
  
      # txn is a Transaction object
      for i in range(len(patches)):
          if use_label:
              item = Item(patches[i], coords[i], labels[i])
          else:
              item = Item(patches[i], coords[i], 0)
  
          p = item.get_patch_as_image()
  
          str_id = file_name + '-' + str(coords[i][0]) + '-' + str(coords[i][1]) + '.png'
          print(str_id)
          with open(str_id, 'wb+') as f:
              f.write(p)
  </code></pre>
  
  <p>Any idea what is going wrong?</p>
  
  <p>Regards</p>