How do you convert "tracks" of SVG images to separate PNG images?

I have an SVG image with 67 separate paths.

Are there libraries / tutorials that will create a separate bitmap, such as PNG for each of these paths, and possibly name them according to the path identifier?

+3
source share
3 answers

I do not know of any libraries that would support this exact workflow directly, but this is probably because at least on the surface it seems trivial. Assuming you are talking about top-level nodes <path/>, then this should be done with any library that can work with XML - just work with raw XML directly and ignore the fact that this particular document is SVG. If you have:

<svg ...>
  <path title="a" d="M 100 100 L 300 100 L 200 300 z" stroke-width="3" />
  <path title="b" d="M 200 100 L 300 100 L 200 300 z" stroke-width="3" />
  <path title="c" d="M 300 100 L 300 100 L 200 300 z" stroke-width="3" />
</svg>

And you want:

[a.svg]
<svg ...>
  <path d="M 100 100 L 300 100 L 200 300 z" stroke="blue" stroke-width="3" />
</svg>

[b.svg]
<svg ...>
  <path d="M 200 100 L 300 100 L 200 300 z" stroke="blue" stroke-width="3" />
</svg>

// "", title , ( - , ) . XML API DOM ( XSLT/XPATH ..).

+3

. . Ruby script SVG , batik png . - , SVG. , !

script:

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
+3

Apache Batik - Java SVG. PNG , DOM API, display none , , , Transcoder API . SVG display inline .

+1

Source: https://habr.com/ru/post/1723738/


All Articles