dtreeviz icon indicating copy to clipboard operation
dtreeviz copied to clipboard

Create PNG not just SVG objects

Open parrt opened this issue 2 years ago • 7 comments

Some notebook servers seem to run out of memory or something with huge SVG plots. Also we can't save PNG at this point. This snippet could be injected analogous to:

    def _repr_svg_(self):
        return self.svg()
import cairosvg
import IPython.display as display

v = viz_model.view()
p = cairosvg.svg2png(v.svg().encode(), scale=2)
display.Image(p, retina=True)

parrt avatar Jan 20 '23 21:01 parrt

@parrt this code works well for me to save a viz to png or pdf.

I know the README says:

Limitations. Only svg files can be generated at this time, which reduces dependencies and dramatically simplifies install process.

but maybe we can include this as an example script somewhere, if we don't want to make wand, svgib, and reportlab dependencies?

from wand.image import Image
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF

def save_dtreeviz(viz, m_path, fname, tag='', inline=False, svg=False, png=True, pdf=False):
    if inline:
        display(viz)
    else:
        if not (svg or png or pdf):
            warnings.warn('Not saving anything!')

        os.makedirs(m_path, exist_ok=True)
        full_path = f'{m_path}/{fname}{tag}'

        # svg
        viz.save(f'{full_path}.svg')

        # pdf via svglib
        if pdf:
            renderPDF.drawToFile(svg2rlg(f'{full_path}.svg'), f'{full_path}.pdf')

        # png via wand / ImageMagick
        if png:
            img = Image(filename=f'{full_path}.svg', resolution=500)
            img.format = 'png'
            img.save(filename=f'{full_path}.png')

        if not svg:
            # clean up svg file
            os.remove(f'{full_path}.svg')

        # clean up graphviz dot file (no extension)
        os.remove(full_path)

mepland avatar Jan 21 '23 21:01 mepland

would be nice to incorporate...how common/popular are those libs? add to the pip install options?

parrt avatar Jan 21 '23 22:01 parrt

They have pip installed just fine for me in the past, much easier than graphviz.

mepland avatar Jan 21 '23 23:01 mepland

I wonder if that's true on UNIX and windows. If so, let's go for it and integrate. I think there is a _repr_png_() we can fill in inside our visualization object...

parrt avatar Jan 22 '23 17:01 parrt

It has been working for me on Linux (Fedora) and mac.

It actually saves the svg to disk, then reloads and converts it, so there maybe a more efficient solution. I just know this has been working and has been easy to graft on to the library without changing any underlying code.

mepland avatar Jan 22 '23 18:01 mepland

I think the solution above using cairo works. Is there a reason not to go that route? It doesn't need to save to the disk for example.

parrt avatar Jan 22 '23 19:01 parrt

That's fine with me, I just wanted to share what I was doing.

Is it possible to have options to also save as a pdf, and clean up the dot file, like I do above?

mepland avatar Jan 23 '23 03:01 mepland