Skip to content

Instantly share code, notes, and snippets.

@tamouse
Created December 6, 2015 04:23
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 tamouse/679896e43278b900ba2c to your computer and use it in GitHub Desktop.
Save tamouse/679896e43278b900ba2c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Make a collection of gallery images:
# - thumbnails: 200x200 square
# - gallery: 450 horizontal
# - web: 1024 horizontal
require 'fileutils'
require 'yaml'
require 'pathname'
DEFAULTS = {
thumbs: { dir: 'thumbs', geom: '200x200', format: 'gif', quality: 40, options: '-thumbnail %{geom}^ -gravity center -extent %{geom}' },
gallery: { dir: 'gallery', geom: '450', format: 'jpg', quality: 60, options: '-resize %{geom} -quality %{quality}' },
web: { dir: 'webs', geom: '1024', format: 'jpg', quality: 75, options: '-resize %{geom} -quality %{quality}' }
}
def imagefiles(start='.')
@imagefiles ||= Dir[File.join(start),'*'].select{|f| f =~ /\.(jpe?g|png|gif)$/ }
end
def process(dir, geom, format, quality, options, start='.')
options = options % {
geom: geom,
quality: quality
}
Dir.chdir(start) do |root|
FileUtils.mkdir_p(dir)
cmd = %Q{mogrify -verbose -path #{dir} -format #{format} #{options} #{imagefiles.join(" ")}}
`#{cmd}`
end
end
def get_image_path(dir='.')
full_path = Pathname.new(dir).expand_path().to_s
remove_path = Pathname(ENV["HOME"]).join("Art").to_s + ?/
(full_path.sub(/#{remove_path}/, '') + ?/)
end
def main(galleries=nil, start='.')
galleries.each do |type, options|
puts "Making #{type}"
process(options[:dir], options[:geom], options[:format], options[:quality], options[:options], start)
puts "done\n"
end
end
def yamlize(start)
{
"gallery" => {
"path" => get_image_path(start),
"images" => imagefiles.map do |f|
{
"fullsize" => f,
"gallery" => "gallery/#{f}",
"web" => "webs/#{f}",
"thumb" => "thumbs/#{File.basename(f,'.*')}.gif",
"caption" => File.basename(f),
"description" => "
All about the image #{f}
"
}
end
}
}.to_yaml
end
start = Dir.pwd
galleries = DEFAULTS
puts "\nProcessing directory #{start}\n"
main(galleries, start)
puts "\nComplete"
print "\nCreating yaml entry"
File.write("gallery.yml", yamlize(start))
puts " .. done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment