Skip to content

Instantly share code, notes, and snippets.

@msaffitz
Created December 1, 2011 00:50
Show Gist options
  • Save msaffitz/1412348 to your computer and use it in GitHub Desktop.
Save msaffitz/1412348 to your computer and use it in GitHub Desktop.
Extract OTZ images from a PG database and create them in the filesystem
require 'rubygems'
require 'pg'
require 'FileUtils'
def write_file(data, path, name)
return if data.nil? or data.empty?
return if File.exists?("#{path}/#{name}")
FileUtils.mkdir_p path
f = File.new("#{path}/#{name}", 'w')
f.write(PGconn.unescape_bytea(data))
f.close
end
conn = PGconn.open(:dbname => 'otz')
puts "Fetching images ... "
images = conn.exec('select * from images')
puts "Extracting #{images.count} images ... "
images.each_with_index do |image, index|
puts " (#{index+1}/#{images.count}) .. Extracting #{image['img_file_name']}"
path = "extracted_images/#{image['id']}"
write_file(image['img_file'], "#{path}/original", image['img_file_name'])
write_file(image['img_medium_file'], "#{path}/medium", image['img_file_name'])
write_file(image['img_thumb_file'], "#{path}/thumb", image['img_file_name'])
end
puts "Complete."
conn.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment