ecceman / affinity

Free 2D symbols for computer network diagrams

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convert all files to PNG

ecceman opened this issue · comments

I made you a bash script to automate the task:

#!/bin/bash

if [ -n "${1}" ]
then
	if [ ! -d ${1} ]
	then
		echo "Directory not found."
		exit 1
	fi
else
	echo -e "You must provide the directory containing the Affinity SVGs.\nFor example: ./convertAffinitySvg.sh /home/toto/Downloads/affinity/svg"
	exit 1
fi

extention_src="svg"
extention_dst="png"
export_dpi=300
rep_affinity_src="${1}"
rep_affinity_dst="/tmp/affinity/${extention_dst}/"

cd ${rep_affinity_src}

creatRepDst()
{
for rep in $(find . -mindepth 1 -type d); do
	mkdir -p ${rep_affinity_dst}/${rep}
done
}

convertImg()
{
for convert in $(find . -mindepth 1 -name "*.${extention_src}"); do
	suppr_extension=${convert%.*}
	inkscape ${convert} -o ${rep_affinity_dst}/${suppr_extension}.${extention_dst} --export-type=${extention_dst} --export-dpi=${export_dpi}
done
}

creatRepDst
convertImg

You have to use it like this: ./convertAffinitySvg.sh ~/Downloads/affinity/svg

It creates a /tmp/affinity/png/ directory, creates the Affinity SVG tree and converts (by copying) the SVGs to PNG or any other format supported by Inkscape (svg,png,ps,eps,pdf,emf,wmf,xaml) by changing the value of the variable extension_dst (the created directory adapts to the extension).

I use Inkscape for conversion. There may be more optimized. I'll see...
If you have any questions, don't hesitate ;)

FYI, a bit late to the party. Also made a fork and python script for converting to PNG and size for the popular EVE-NG network simulation tool. https://github.com/JulioPDX/affinity/blob/master/convert.py

#!/usr/bin/env python

"""
Script used to convert a bunch of SVG files to PNG
"""

from os import walk, mkdir
import pyvips

# sudo apt-get install libvips
# pip install pyvips

PATH = "svg/"


def main():

    """
    All the action
    """

    for (root, dirs, files) in walk(PATH):
        for f in files:
            if ".svg" in f:
                try:
                    mkdir(path=f"{root}/png/")
                except OSError as error:
                    pass
                convert_file = pyvips.Image.thumbnail(f"{root}/{f}", 52, height=52)
                convert_file.write_to_file(f"{root}/png/{f.replace('svg', 'png')}")


if __name__ == "__main__":
    main()

@JulioPDX your useful script worked for me with cairosvg instead of pyvips. Thanks for that!

cairosvg.svg2png(url=f"{root}/{f}", write_to=f"{root}/png/{f.replace('svg', 'png')}")

@dalcacer Wow! I'm very happy I could be of help! I think I initially attempted with cairo but had some issue. Probably user error.