Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'time'
require 'date'
PHOTOS_DIR = "/Users/yourname/yoursite/_assets/images/photos"
POSTS_DIR = "/Users/yourname/yoursite/_posts"
def exif_value( data, key )
r = Regexp.new( "exif:#{key}:(.+)")
result = r.match( data )[1].strip rescue nil
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on('-f', '--file NAME', 'File') { |v| options[:file] = v }
end.parse!
puts options[:file]
data = `identify -verbose "#{options[:file]}"`
puts data
exif = {}
exif[:model] = exif_value( data, "Model")
exif[:desc] = exif_value( data, "ImageDescription")
exif[:iso] = exif_value( data, "ISOSpeedRatings")
exif[:fstop] = exif_value( data, "FNumber")
exif[:shutter] = exif_value( data, "ExposureTime")
exif[:title] = /Image Name(.*):(.*)/.match(data)[2].strip rescue nil
puts exif
filename_sans_extension = options[:file].split( '.' ).first.split( '/' ).last
photo_date = Time.parse( /date:create:(.*)/.match(data)[1].strip ) rescue nil
timestamp_string = Time.now.strftime("%Y-%m-%d-%-d-%-m")
lg_output_filename = "#{timestamp_string}-#{filename_sans_extension.gsub( ' ', '-' )}.jpg"
sm_output_filename = "#{timestamp_string}-#{filename_sans_extension.gsub( ' ', '-' )}-sm.jpg"
cp_cmd = "cp \"#{options[:file]}\" \"#{PHOTOS_DIR}/#{lg_output_filename}\""
sm_cmd = "convert \"#{options[:file]}\" -resize 1200x2000 -quality 60 \"#{PHOTOS_DIR}/#{sm_output_filename}\""
puts cp_cmd
`#{cp_cmd}`
puts sm_cmd
`#{sm_cmd}`
photo_post_md = <<-EOF
---
layout: photo-post
title: "#{exif[:title]}"
date: #{Time.now.strftime("%Y-%m-%d")}
category: photo
image: photos/#{sm_output_filename}
photo_sm: #{sm_output_filename}
photo_lg: #{lg_output_filename}
exif_model: #{exif[:model]}
exif_desc: #{exif[:desc]}
exif_iso: #{exif[:iso]}
exif_fstop: #{exif[:fstop]}
exif_shutter: #{exif[:shutter]}
exif_title: #{exif[:title]}
exif_datestring: #{photo_date.strftime("%B %-d, %Y")}
---
EOF
post_filename = "#{POSTS_DIR}/#{Time.now.strftime("%Y-%m-%d")}-photo-#{exif[:title].gsub( ' ', '-' ).downcase}.md"
puts post_filename
puts photo_post_md
File.open(post_filename, 'w') {|f| f.write(photo_post_md) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment