title: How to convert Matplotlib figure to PIL Image object (without saving image)
body: <p>As the title states, I am trying to convert a <code>fig</code> to a <code>PIL.Image</code>. I am currently able to do so by first saving the <code>fig</code> to disk and then opening that file using <code>Image.open()</code> but the process is taking longer than expected and I am hoping that by skipping the saving locally step it will be a bit faster.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code># build fig
  figsize, dpi = self._calc_fig_size_res(img_height)
  fig = plt.Figure(figsize=figsize)
  canvas = FigureCanvas(fig)
  ax = fig.add_subplot(111)
  ax.imshow(torch.from_numpy(S).flip(0), cmap = cmap)
  fig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 1)
  ax.axis('tight'); ax.axis('off')
  
  # export
  fig.savefig(export_path, dpi = dpi)
  
  # open image as PIL object
  img = Image.open(export_path)
  </code></pre>
  
  <p>I have tried doing this after I build the fig (it would be right before the export stage):</p>
  
  <pre><code>pil_img = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
  </code></pre>
  
  <p>But it's not showing the entire image. It looks like it's a crop of the top left corner, but it could just be a weird representation of the data -- I'm working with spectrograms so the images are fairly abstract.</p>