Python 更改 SVG 中的颜色并导出为 SVG/PNG/PDF

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

我有一个在 Inkscape 中创建的 SVG。 我需要制作多个颜色略有不同的 SVG 副本。 我宁愿不手动执行此操作。 python 中是否有一个库允许我 select SVG 中的对象更改其填充颜色,然后使用新颜色保存 SVG 的副本或将其导出到 png/pdf ?

一旦我意识到 SVG 基本上是大型文本文件,我就设法找到了一种相当简单的方法来执行此操作。 我还发现 CairoSVG 可以将它们导出到 png 文件中。 下面的代码读取给定文件夹中的所有 SVG 文件,然后为颜色数组中的每种颜色导出 png 版本。

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

根据您的 Inkscape 版本,有两个扩展可用:

  1. 生成器(0.92 及之前版本):http://wiki.colivre.net/Aurium/InkscapeGenerator / Example files: https://inkscape.org/~Moini/%E2%98%85example-files-for-generator-extension

  2. NextGenerator(1.0 及更高版本):https://gitlab.com/Moini/nextgenerator

感谢Talon06,我们知道 svg 只是一大堆写成文本的指令(使用 .svg 扩展名)来指示程序相应地绘制图片。因此,为了简化他的代码和我的理解,这就是您将得到的:

首先读取svg文件:

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

然后替换你选择的颜色的十六进制编解码器

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

就是这样!

请原谅我的英式拼写,我不得不这样做。