Skip to content

Instantly share code, notes, and snippets.

@r10r
Created May 29, 2012 06:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save r10r/2822884 to your computer and use it in GitHub Desktop.
Save r10r/2822884 to your computer and use it in GitHub Desktop.
Extract PNG data from .skitch SVG files
#!/usr/bin/env ruby
# extracts the embedded PNG data from .skitch SVG files
require 'nokogiri'
require 'base64'
IMAGE_HEADER = "data:image/png;base64,"
ARGV.each do |filename|
svg_document = Nokogiri::XML(File.open(filename))
# get rid of the namespaces, facilitates the xpath search
svg_document.remove_namespaces!
# retrieve the href attribute that contains the image data
image_attribute = svg_document.xpath("//image/@href")[0]
# remove the header
image_data = image_attribute.value.slice(IMAGE_HEADER.length..-1)
png_filename = filename.gsub(/(.*)(\.skitch)/,'\1.png')
File.write(png_filename, Base64.decode64(image_data))
end
@r10r
Copy link
Author

r10r commented Feb 20, 2013

Maybe I should create a gem for that

@boie0025
Copy link

I wound up needing something like this for a project, started a gem here: https://github.com/boie0025/svg_inline_file_extractor

Thanks for the head start!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment