Skip to content

Instantly share code, notes, and snippets.

@MitchellKehn
Created August 10, 2021 11:00
Show Gist options
  • Save MitchellKehn/a3a0227a6450d644beec12f04a013dd7 to your computer and use it in GitHub Desktop.
Save MitchellKehn/a3a0227a6450d644beec12f04a013dd7 to your computer and use it in GitHub Desktop.
[Batch Convert SVG-> PNG] uses PySide2 to convert SVGs to PNGs
from PySide2 import QtGui, QtSvg
import os
FOLDER = r"C:\Users\mkehn\Desktop\test_theme\theme\primary"
def convertPngToSvg(filepath, size):
folder, filename = os.path.split(filepath)
basename, ext = os.path.splitext(filename)
image = QtGui.QImage(size, size, QtGui.QImage.Format_ARGB32)
image.fill(0)
painter = QtGui.QPainter()
painter.begin(image)
renderer = QtSvg.QSvgRenderer()
renderer.load(filepath)
renderer.render(painter)
painter.end()
image.save(os.path.join(folder, basename+".png"))
for filename in os.listdir(FOLDER):
if filename.endswith("svg"):
convertPngToSvg(os.path.join(FOLDER, filename), 48)
print filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment