Skip to content

Instantly share code, notes, and snippets.

@Juknum
Last active June 25, 2024 18:11
Show Gist options
  • Save Juknum/d69ed5bb6e6dc6cc927702ece66a9164 to your computer and use it in GitHub Desktop.
Save Juknum/d69ed5bb6e6dc6cc927702ece66a9164 to your computer and use it in GitHub Desktop.
Small Image to SVG Converter

📕 Requirements

This program require Python 3 and the Pillow module which you can install like so:

python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

🧾 Usage

To use this program, just drag and drop any .png file on the program.bat file.

⚠️ Warning

  1. I do not recommend to go above 64x64 base file resolution as the output become very laggy when the file is bigger
  2. You might even reach the object limit a SVG file can support
python script.py %*
import sys
from PIL import Image
for arg in sys.argv[1:]:
img = Image.open(arg)
img = img.convert('RGBA')
output = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" ><g>'.format(width=img.width, height=img.height)
for x in range(0, img.width):
for y in range(0, img.height):
r, g, b, a = img.getpixel((x, y))
if a != 0:
color = '#%02x%02x%02x' % (r, g, b)
output += '<rect x="{x}" y="{y}" width="1.1" height="1.1" fill="{color}" />'.format(x = x - .1, y = y - .1, color=color)
output += '</g></svg>'
with open('{arg}.svg'.format(arg=arg.replace('.png', '')), 'w') as f:
f.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment