Search code examples
pythonnlp

How to save the output of constituency parsing diagram as an image?


Using the svgling module I have generated the constituency parse tree. Here is the github link of svgling: svgling module github

pip install svgling
import svgling
import nltk
var ='(S (NP this tree) (VP (V is) (AdjP pretty)))'
svgling.disable_nltk_png()
random=svgling.draw_tree(nltk.Tree.fromstring(var))
display(random)

Using this code I have got the diagram as an output (given below) of the constituency parse tree. I want to generate .png file of this output. Constituency parse tree (Output of the above code)

To save the output as .png file I have run this following code.

import os
from nltk.tree import Tree
from nltk.draw.tree import TreeView
from nltk.draw.util import CanvasFrame
from nltk.draw import TreeWidget
os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0' 
t = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))')
cf = CanvasFrame()
TreeView(t)._cframe.print_to_file('output.ps')
os.system('convert output.ps output.png')

However, this code is showing error in google colab TclError: couldn't connect to display ":1.0" and returning a numerical value (ex: 4) in jupyter notebook.

I tried to look into it on several websites but couldn't find any solution.


Solution

  • Once I also had to convert the constituency parsing diagram as an image. To do so, at first I have converted it into PostScript (.ps) file. And, from .ps file converted it into .png file.

    To convert the tree diagram into the .ps file I ran the following code. This will give output.ps file.

    from nltk.tree import Tree
    from nltk.draw.tree import TreeView
    t = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))')
    TreeView(t)._cframe.print_to_file('output.ps')
    

    However, I was trying to run the code on google colab. And it was giving the error:

    TclError: no display name and no $DISPLAY environment variable in Google Colab

    To resolve the issue, ran the following code:

    !apt-get install -y xvfb # Install X Virtual Frame Buffer
    import os
    os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
    os.environ['DISPLAY']=':1.0'
    

    Now to convert output.ps file into output.png:

    !apt install ghostscript python3-tk
    from PIL import Image
    psimage=Image.open('output.ps')
    psimage.save('output.png')
    

    However, could not manage to get very good image quality in this way.