Talon06
Talon06

Reputation: 1796

Python change Color in SVG and Export to SVG/PNG/PDF

I have a SVG I created in Inkscape. I need to make multiple copies of the SVG with slightly different colors. I'd rather not do this manually. Is there a library in python that would allow me to select the object in the SVG change its fill color then either save a copy of the SVG with the new color or export it to png/pdf?

Upvotes: 1

Views: 8194

Answers (3)

Ronan Hansel
Ronan Hansel

Reputation: 138

Thanks to Talon06, we know svg is just a big pile of instructions written as text (using .svg extension) to instruct the program to draw the picture accordingly. So to simplify his code, and my understanding, this is what you'll get:

firstly read the svg file:

with codecs.open('full-path-to-the-svg-file', encoding='utf-8', errors='ignore') as f:
        content = f.read()

and then replace the hex codec of the colour of your choice

old_colour = 'ffffff'
new_colour = 'ff00ff'
new_SVG = content.replace(old_colour, new_colour)

That's just about it!

Excuse my British spelling, i just have to.

Upvotes: 3

Moini
Moini

Reputation: 1397

Depending on your Inkscape version, there are two extensions available for this:

  1. Generator (0.92 and before): http://wiki.colivre.net/Aurium/InkscapeGenerator / Example files: https://inkscape.org/~Moini/%E2%98%85example-files-for-generator-extension

  2. NextGenerator (1.0 and later): https://gitlab.com/Moini/nextgenerator

Upvotes: 0

Talon06
Talon06

Reputation: 1796

I managed to sort out a fairly simple way to do this once i realized SVG's are basically large text files. I also found CairoSVG to export them in to png files. The code below reads all SVG files in a given folder then exports a png version for each color in the colors array.

import codecs
import cairosvg
import glob
import os
import sys

colors = [
    ["alice blue", "#F0F8FF"],
    ["antique white", "#FAEBD7"],
    ["aqua", "#00FFFF"],
    ["aqua marine", "#7FFFD4"],
    ["azure", "#F0FFFF"],
    ["beige", "#F5F5DC"],
    ["bisque", "#FFE4C4"],
    ["Black", "#000000"],
    ["black", "#000000"],
    ["blanched almond", "#FFEBCD"],
    ["Blue", "#0000FF"],
    ["blue", "#0000FF"],
    ["blue violet", "#8A2BE2"],
    ["brown", "#A52A2A"],
    ["burly wood", "#DEB887"],
    ["cadet blue", "#5F9EA0"],
    ["chart reuse", "#7FFF00"],
    ["chocolate", "#D2691E"],
    ["coral", "#FF7F50"],
    ["corn flower blue", "#6495ED"],
    ["corn silk", "#FFF8DC"],
    ["crimson", "#DC143C"],
    ["cyan", "#00FFFF"],
    ["Cyan-Aqua", "#00FFFF"],
    ["dark blue", "#00008B"],
    ["dark cyan", "#008B8B"],
    ["dark golden rod", "#B8860B"],
    ["dark gray-dark grey", "#A9A9A9"],
    ["dark green", "#006400"],
    ["dark khaki", "#BDB76B"],
    ["dark magenta", "#8B008B"],
    ["dark olive green", "#556B2F"],
    ["dark orange", "#FF8C00"],
    ["dark orchid", "#9932CC"],
    ["dark red", "#8B0000"],
    ["dark red", "#8B0000"],
    ["dark salmon", "#E9967A"],
    ["dark sea green", "#8FBC8F"],
    ["dark slate blue", "#483D8B"],
    ["dark slate gray", "#2F4F4F"],
    ["dark turquoise", "#00CED1"],
    ["dark violet", "#9400D3"],
    ["deep pink", "#FF1493"],
    ["deep sky blue", "#00BFFF"],
    ["dim gray-dim grey", "#696969"],
    ["dodger blue", "#1E90FF"],
    ["firebrick", "#B22222"],
    ["floral white", "#FFFAF0"],
    ["forest green", "#228B22"],
    ["gainsboro", "#DCDCDC"],
    ["ghost white", "#F8F8FF"],
    ["gold", "#FFD700"],
    ["golden rod", "#DAA520"],
    ["Gray", "#808080"],
    ["gray-grey", "#808080"],
    ["Green", "#008000"],
    ["green", "#008000"],
    ["green yellow", "#ADFF2F"],
    ["honeydew", "#F0FFF0"],
    ["hot pink", "#FF69B4"],
    ["indian red", "#CD5C5C"],
    ["indigo", "#4B0082"],
    ["ivory", "#FFFFF0"],
    ["khaki", "#F0E68C"],
    ["lavender", "#E6E6FA"],
    ["lavender blush", "#FFF0F5"],
    ["lawn green", "#7CFC00"],
    ["lemon chiffon", "#FFFACD"],
    ["light blue", "#ADD8E6"],
    ["light coral", "#F08080"],
    ["light cyan", "#E0FFFF"],
    ["light golden rod yellow", "#FAFAD2"],
    ["light gray-light grey", "#D3D3D3"],
    ["light green", "#90EE90"],
    ["light pink", "#FFB6C1"],
    ["light salmon", "#FFA07A"],
    ["light sea green", "#20B2AA"],
    ["light sky blue", "#87CEFA"],
    ["light slate gray", "#778899"],
    ["light steel blue", "#B0C4DE"],
    ["light yellow", "#FFFFE0"],
    ["Lime", "#00FF00"],
    ["lime", "#00FF00"],
    ["lime green", "#32CD32"],
    ["linen", "#FAF0E6"],
    ["Magenta-Fuchsia", "#FF00FF"],
    ["Maroon", "#800000"],
    ["maroon", "#800000"],
    ["medium aqua marine", "#66CDAA"],
    ["medium blue", "#0000CD"],
    ["medium orchid", "#BA55D3"],
    ["medium purple", "#9370DB"],
    ["medium sea green", "#3CB371"],
    ["medium slate blue", "#7B68EE"],
    ["medium spring green", "#00FA9A"],
    ["medium turquoise", "#48D1CC"],
    ["medium violet red", "#C71585"],
    ["midnight blue", "#191970"],
    ["mint cream", "#F5FFFA"],
    ["misty rose", "#FFE4E1"],
    ["moccasin", "#FFE4B5"],
    ["navajo white", "#FFDEAD"],
    ["Navy", "#000080"],
    ["navy", "#000080"],
    ["old lace", "#FDF5E6"],
    ["Olive", "#808000"],
    ["olive", "#808000"],
    ["olive drab", "#6B8E23"],
    ["orange", "#FFA500"],
    ["orange red", "#FF4500"],
    ["orchid", "#DA70D6"],
    ["pale golden rod", "#EEE8AA"],
    ["pale green", "#98FB98"],
    ["pale turquoise", "#AFEEEE"],
    ["pale violet red", "#DB7093"],
    ["papaya whip", "#FFEFD5"],
    ["peach puff", "#FFDAB9"],
    ["peru", "#CD853F"],
    ["pink", "#FFC0CB"],
    ["plum", "#DDA0DD"],
    ["powder blue", "#B0E0E6"],
    ["Purple", "#800080"],
    ["purple", "#800080"],
    ["Red", "#FF0000"],
    ["red", "#FF0000"],
    ["rosy brown", "#BC8F8F"],
    ["royal blue", "#4169E1"],
    ["saddle brown", "#8B4513"],
    ["salmon", "#FA8072"],
    ["sandy brown", "#F4A460"],
    ["sea green", "#2E8B57"],
    ["sea shell", "#FFF5EE"],
    ["sienna", "#A0522D"],
    ["Silver", "#C0C0C0"],
    ["silver", "#C0C0C0"],
    ["sky blue", "#87CEEB"],
    ["slate blue", "#6A5ACD"],
    ["slate gray", "#708090"],
    ["snow", "#FFFAFA"],
    ["spring green", "#00FF7F"],
    ["steel blue", "#4682B4"],
    ["tan", "#D2B48C"],
    ["Teal", "#008080"],
    ["teal", "#008080"],
    ["thistle", "#D8BFD8"],
    ["tomato", "#FF6347"],
    ["turquoise", "#40E0D0"],
    ["violet", "#EE82EE"],
    ["wheat", "#F5DEB3"],
    ["White", "#FFFFFF"],
    ["white", "#FFFFFF"],
    ["white smoke", "#F5F5F5"],
    ["Yellow", "#FFFF00"],
    ["yellow", "#FFFF00"],
    ["yellow green", "#9ACD32"]
]

os.chdir(sys.argv[1])
if len(sys.argv) > 2:
    c2replace = sys.argv[2]
else:
    c2replace = "#ff0066"

for file in glob.glob("*.svg"):
    with codecs.open(file, encoding='utf-8', errors='ignore') as f:
        content = f.read()

    for color in colors:
        newSVG = content.replace(c2replace, color[1])
        newBytes = str.encode(newSVG)
        try:
            cairosvg.svg2png(bytestring=newBytes, write_to="{}_{}.png".format(os.path.splitext(file)[0], color[0]))
        except Exception as e:
            print(e)
            print(color)
            pass

Upvotes: 2

Related Questions