def _figure_to_png()

in chartify/_core/chart.py [0:0]


    def _figure_to_png(self, force_resize=True, scale_factor=1):
        """Convert figure object to PNG
        Bokeh can only save figure objects as html.
        To convert to PNG the HTML file is opened in a headless browser.
        """
        driver = self._initialize_webdriver()
        # Save figure as HTML
        html = file_html(self.figure, resources=INLINE, title="")
        fp = tempfile.NamedTemporaryFile("w", prefix="chartify", suffix=".html", encoding="utf-8")
        fp.write(html)
        fp.flush()
        # Open html file in the browser.
        driver.get("file:///" + fp.name)
        driver.execute_script("document.body.style.margin = '0px';")
        image = get_screenshot_as_png(self.figure, driver=driver, scale_factor=scale_factor)
        driver.quit()
        fp.close()
        # Resize image if necessary.
        if force_resize:
            target_dimensions = (self.style.plot_width, self.style.plot_height)
            if image.size != target_dimensions:
                image = image.resize(target_dimensions, resample=Resampling.LANCZOS)
        return image