Skip to content

Instantly share code, notes, and snippets.

@sandrewh
Created May 3, 2012 21:34
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 sandrewh/2589699 to your computer and use it in GitHub Desktop.
Save sandrewh/2589699 to your computer and use it in GitHub Desktop.
extract thumbnail from Paint.NET file
require "base64"
# check for input file parameter
puts "ruby pdn_decode.rb infile.pdn" or exit if ( ARGV.length!=1 || File.extname(ARGV[0]).downcase!='.pdn' || !File.exist?(ARGV[0]) )
# extract image type and image data
imageType, base64Data = IO.read(ARGV[0]).match(/<thumb (.+?)="(.+?)" \/>/)[1..2]
# determine new filename and check that it doesn't exist
newFile = ARGV[0][0..-5] + ".#{imageType}"
puts "#{newFile} already exists." or exit if File.exists? newFile
# save image data to new file
File.open(newFile, "w") {|f| f.write(Base64.decode64(base64Data))}
# open file -- os x specific
system `open #{newFile}` if RUBY_PLATFORM=~/darwin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment