如何将 SVG 图像“路径”转换为单独的 PNG 图像?

bte*_*les 3 svg imagemagick image-processing rsvg librsvg

我有一个带有 67 个单独路径的 SVG 图像。

是否有任何库/教程可以为每个路径创建单独的光栅图像,例如 PNG,并且可能根据路径 ID 命名它们?

bte*_*les 5

谢谢大家。我最终使用了两个答案的组合。我使用 Ruby 脚本来解析 SVG 文件并为每个路径编写一个新文件,然后使用蜡染转换器从该文件生成一个 png。接受的答案是 Nick,因为它的关键见解是创建单个 SVG 很容易。谢谢尼克!

这是脚本:

require 'rubygems'
require 'nokogiri'
file = 'dark_florida_no_numbers.svg'

file_string = IO.read(file)

reader = Nokogiri::XML::Reader(file_string)

reader.each do |node|

  if node.name == 'path'
    county = node.attributes['id']
    File.open("dark/#{county}.svg", 'w') do |f|
      f.write "<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" >\n  <path "
      node.attributes.each do |attr, value|
        f.write " #{attr}=\"#{value}\""
      end
      f.write "/>\n</svg>"
    end
    `java -jar b/batik-rasterizer.jar dark/#{county}.svg`
  end
end
Run Code Online (Sandbox Code Playgroud)