Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Forked from r10r/skitch2png.rb
Last active December 14, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhandley/5014904 to your computer and use it in GitHub Desktop.
Save seanhandley/5014904 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# extracts the embedded PNG data from .skitch SVG files
require 'nokogiri'
require 'base64'
require 'fileutils'
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))
FileUtils.touch(png_filename, :mtime => File.ctime(filename))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment