title: How to save "IPython.core.display.SVG" as PNG file?
body: <p>I am trying to save a variable with data-type of &quot;IPython.core.display.SVG&quot; as a PNG file in Jupyter Notebook environment.</p>
  <p>First I tried:</p>
  <pre><code>with open('./file.png','wb+') as outfile:
      outfile.write(my_svg.data)
  </code></pre>
  <p>And I got the error:</p>
  <pre><code>TypeError: a bytes-like object is required, not 'str'
  </code></pre>
  <p>Next, I tried:</p>
  <pre><code>with open('./file.png','wb+') as outfile:
      outfile.write(my_svg.data.encode('utf-8'))
  </code></pre>
  <p>But, I cannot open &quot;file.png&quot;. The operating system gives error:</p>
  <pre><code>The file “file.png” could not be opened. It may be damaged or use a file format that Preview doesn’t recognize.
  </code></pre>
  <p>I can save &quot;my_svg&quot; with &quot;svg&quot; extension as below:</p>
  <pre><code>with open('./file.svg','wb+') as outfile:
      outfile.write(my_svg.data.encode('utf-8'))
  </code></pre>
  <p>But, when I want to convert &quot;file.svg&quot; into &quot;file.png&quot; by:</p>
  <pre><code>import cairosvg
  cairosvg.svg2png(url=&quot;./file.svg&quot;, write_to=&quot;./file.png&quot;)
  </code></pre>
  <p>I get the error:</p>
  <pre><code>ValueError: unknown locale: UTF-8
  </code></pre>
  <p>This is how I get &quot;IPython.core.display.SVG&quot; data-type in Jupyter Notebook:</p>
  <pre><code>from rdkit import Chem
  from rdkit.Chem.Draw import rdMolDraw2D
  from IPython.display import SVG
  
  
  smile_1 = 'C(C(N)=O)c(c)c'
  smile_2 = 'o(cn)c(c)c'
  
  m1 = Chem.MolFromSmiles(smile_1,sanitize=False)
  Chem.SanitizeMol(m1, sanitizeOps=(Chem.SanitizeFlags.SANITIZE_ALL^Chem.SanitizeFlags.SANITIZE_KEKULIZE^Chem.SanitizeFlags.SANITIZE_SETAROMATICITY))
  m2 = Chem.MolFromSmiles(smile_2,sanitize=False)
  Chem.SanitizeMol(m2, sanitizeOps=(Chem.SanitizeFlags.SANITIZE_ALL^Chem.SanitizeFlags.SANITIZE_KEKULIZE^Chem.SanitizeFlags.SANITIZE_SETAROMATICITY))
  
  mols = [m1, m2]
  legends = [&quot;smile_1&quot;, &quot;smile_2&quot;]
  
  molsPerRow=2
  subImgSize=(200, 200)
  nRows = len(mols) // molsPerRow
  if len(mols) % molsPerRow:
    nRows += 1
    
  fullSize = (molsPerRow * subImgSize[0], nRows * subImgSize[1])
  d2d = rdMolDraw2D.MolDraw2DSVG(fullSize[0], fullSize[1], subImgSize[0], subImgSize[1])
  d2d.drawOptions().prepareMolsBeforeDrawing=False
  d2d.DrawMolecules(list(mols), legends=legends)
  d2d.FinishDrawing()
  SVG(d2d.GetDrawingText())
  </code></pre>
  <p>Environment:</p>
  <ul>
  <li>macOS 11.2.3</li>
  <li>python 3.6</li>
  <li>RDKit version 2020.09.1</li>
  </ul>
  <p>Any help is greatly appreciated.</p>