Create a custom service to convert SVG to PNG

Hi.

I want to create a custom components that converts an svg from a URL to a local png file.

Something like this. but i have been trying to get som help from OpenAI, but i am stuck.
is there someone that can help me with this.

the URL i want to convert is this:

https://www.yr.no/nb/innhold/1-36072/meteogram.svg

and i need it to be called as a service.


import requests
import io
import cairosvg
import os
from homeassistant.helpers.aiohttp_client import async_get_clientsession

async def convert_svg_to_png(svg_url, output_dir):
    session = async_get_clientsession()

    try:
        response = await session.get(svg_url)
        if response.status == 200:
            svg_data = await response.read()
            png_data = cairosvg.svg2png(bytestring=svg_data)

            # Save the PNG file to the specified directory
            png_file_path = os.path.join(output_dir, 'meteogram.png')
            with open(png_file_path, 'wb') as png_file:
                png_file.write(png_data)

            return png_file_path
        else:
            return None
    except Exception as e:
        return None

Replying as I’d like to know what the best options are.

As a newbie, I’m familiar with Node Red and Imagemagick so I’d probably try get them to collaborate.

Steve

Take a look at the developer docs for custom services

Hi @birga did you managed to use the meteogram.svg? If yes, could you please describe how it works?