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

Convert all files to PNG #12

Open
ecceman opened this issue Apr 2, 2020 · 4 comments
Open

Convert all files to PNG #12

ecceman opened this issue Apr 2, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@ecceman
Copy link
Owner

ecceman commented Apr 2, 2020

No description provided.

@ecceman ecceman added the enhancement New feature or request label Apr 2, 2020
@ecceman ecceman self-assigned this Apr 2, 2020
@ycharbi
Copy link

ycharbi commented Apr 30, 2020

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

@JulioPDX
Copy link

JulioPDX commented Apr 7, 2021

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

@dalcacer
Copy link

@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')}")

@JulioPDX
Copy link

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

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

4 participants