Skip to content

Instantly share code, notes, and snippets.

@ilyar
Last active March 9, 2021 11:09
Show Gist options
  • Select an option

  • Save ilyar/fd5e109924b2cb3feed80fb1fe596495 to your computer and use it in GitHub Desktop.

Select an option

Save ilyar/fd5e109924b2cb3feed80fb1fe596495 to your computer and use it in GitHub Desktop.
Convert png to svg
#!/usr/bin/env bash
# Install
# wget https://git.io/JqtZd -O ~/.local/bin/png2svg && chmod +x ~/.local/bin/png2svg
if [ "$1" == "" ]; then
echo "Usage: $0 png file"
exit 0;
fi
filename=$(basename "$1" .png)
if [ ! -e "$filename.png" ]; then
echo "$filename.png does not exist"
exit 1;
fi
width=$(identify -format '%w' "$filename.png")
height=$(identify -format '%h' "$filename.png")
base64=$(cat "$filename.png" | base64 -w0)
image=$(printf '<image width="%s" height="%s" preserveAspectRatio="none" style="image-rendering:optimizeQuality" xlink:href="data:image/png;base64,%s"/>' "$width" "$height" "$base64")
printf '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="%s" height="%s">%s</svg>' "$width" "$height" "$image" > "$filename.svg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment