【发布时间】:2023-12-24 04:08:01
【问题描述】:
我正在尝试在 Jupyter Notebook 环境中将数据类型为“IPython.core.display.SVG”的变量保存为 PNG 文件。
首先我尝试过:
with open('./file.png','wb+') as outfile:
outfile.write(my_svg.data)
我得到了错误:
TypeError: a bytes-like object is required, not 'str'
接下来,我尝试了:
with open('./file.png','wb+') as outfile:
outfile.write(my_svg.data.encode('utf-8'))
但是,我无法打开“file.png”。操作系统报错:
The file “file.png” could not be opened. It may be damaged or use a file format that Preview doesn’t recognize.
我可以使用“svg”扩展名保存“my_svg”,如下所示:
with open('./file.svg','wb+') as outfile:
outfile.write(my_svg.data.encode('utf-8'))
但是,当我想通过以下方式将“file.svg”转换为“file.png”时:
import cairosvg
cairosvg.svg2png(url="./file.svg", write_to="./file.png")
我得到错误:
ValueError: unknown locale: UTF-8
这就是我在 Jupyter Notebook 中获得“IPython.core.display.SVG”数据类型的方式:
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 = ["smile_1", "smile_2"]
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())
环境:
- macOS 11.2.3
- python 3.6
- RDKit 版本 2020.09.1
非常感谢任何帮助。
【问题讨论】:
标签: svg jupyter-notebook ipython png rdkit