Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create PNG not just SVG objects #251

Open
parrt opened this issue Jan 20, 2023 · 7 comments
Open

Create PNG not just SVG objects #251

parrt opened this issue Jan 20, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@parrt
Copy link
Owner

parrt commented Jan 20, 2023

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 parrt added the enhancement New feature or request label Jan 20, 2023
@mepland
Copy link
Collaborator

mepland commented Jan 21, 2023

@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)

@parrt
Copy link
Owner Author

parrt commented Jan 21, 2023

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

@mepland
Copy link
Collaborator

mepland commented Jan 21, 2023

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

@parrt
Copy link
Owner Author

parrt commented Jan 22, 2023

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...

@mepland
Copy link
Collaborator

mepland commented Jan 22, 2023

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.

@parrt
Copy link
Owner Author

parrt commented Jan 22, 2023

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.

@mepland
Copy link
Collaborator

mepland commented Jan 23, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants