kyper-data / python-highcharts

A simple translation layer between Python and Javascript for Highcharts projects (highcharts, highmaps, and highstocks).
MIT License
395 stars 190 forks source link

how to save Python-highcharts .html to image #85

Open nankaimy opened 2 years ago

nankaimy commented 2 years ago

how to save Python-highcharts result .html to image

ashokMafiree commented 1 year ago

driver.get('data:text/html;charset=utf-8,' +chart.htmlcontent)

Get the dimensions of the chart container

chart_container = driver.find_element(By.ID, 'container') chart_width = chart_container.size['width'] chart_height = chart_container.size['height']

Take a screenshot of the chart

screenshot = driver.get_screenshot_as_png() screenshot_image = Image.open(BytesIO(screenshot))

Crop the screenshot to the dimensions of the chart

left = chart_container.location['x'] top = chart_container.location['y'] right = left + chart_width bottom = top + chart_height chart_image = screenshot_image.crop((left, top, right, bottom))

Save the chart image to a file

chart_image.save('chart.png')