Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created December 24, 2013 15:50
Show Gist options
  • Select an option

  • Save botanicus/8114994 to your computer and use it in GitHub Desktop.

Select an option

Save botanicus/8114994 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
# Usage:
# nef2jpg.rb DSC_5069.NEF
# nef2jpg.rb DSC_50??.NEF
# nef2jpg.rb .
# settings
PROCESSES = 4
NICE = 20
UFRAW_ARGUMENTS = {
wb: "camera",
exposure: "auto",
saturation: "auto",
black_point: "auto",
compression: 85,
out_type: "jpg",
silent: true
}
# assertions
abort "Did you backed up your photos?!"
# case NICE
# when (-20..-10), (10..20)
# warn "You set NICE to #{NICE}, but this value requires root permissions. Re-running with sudo ..."
# exec "sudo #{$0} #{ARGV.map { |path| '#{path}' }.join(" ")}"
# else
# abort "NICE can't be #{NICE}!"
# end
# helpers
def UFRAW_ARGUMENTS.to_arguments
self.inject(Array.new) do |buffer, pair|
key, value = pair
key = key.to_s.gsub("_", "-")
if pair.last.eql?(true)
buffer << "--#{key}"
elsif pair.last.eql?(false)
buffer << "--no#{key}"
else
buffer << "--#{key}=#{value}"
end
end.join(" ")
end
# Expand directories in ARGV to list of files.
# This is NOT recursive.
files = ARGV.inject(Array.new) do |buffer, path|
if File.directory?(path)
files = Dir.glob("#{path}/*.NEF")
buffer.unshift(*files)
else
buffer << path
end
end
n = files.length / PROCESSES
queues = Array.new
until files.empty?
queues << files.slice!(0..n)
end
# We really need to utilize multiple processors,
# but the GIL in Ruby basically makes threads kind
# of useless, so let's run multiple processes.
queues.each do |queue|
fork do
files = queue.map { |path| "'#{path}'" }
arguments = UFRAW_ARGUMENTS.to_arguments
# command = "nice -n #{NICE} ufraw-batch #{arguments} #{files.join(" ")}" # ufraw-batch do some weird stripe
# command = "nice -n #{NICE} dcraw #{files.join(" ")}"
command = files.inject(String.new) do |buffer, file|
buffer += "nice -n #{NICE} dcraw #{file}"
buffer += "; convert #{file.sub(/\.NEF/, ".ppm")} #{file.sub(/\.NEF/, ".jpg")}"
buffer += "; rm #{file.sub(/\.NEF/, ".ppm")}"
end
puts command
system command
end
end
Process.waitall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment