Class: Pano::ImageFileSet

Inherits:
Object
  • Object
show all
Defined in:
lib/pano/image_file_set.rb

Constant Summary collapse

BRACKETS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_path) ⇒ ImageFileSet

Returns a new instance of ImageFileSet.



8
9
10
11
12
13
# File 'lib/pano/image_file_set.rb', line 8

def initialize dir_path
  @base_path = dir_path
  paths = Dir[File.join(@base_path,"*.JPG")]
  @files = paths.map {|path| ImageFile.new path }
  @files.sort_by &:created_at
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/pano/image_file_set.rb', line 6

def base_path
  @base_path
end

#filesObject (readonly)

Returns the value of attribute files.



6
7
8
# File 'lib/pano/image_file_set.rb', line 6

def files
  @files
end

Instance Method Details

#enfuse(dir, name, files = []) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pano/image_file_set.rb', line 97

def enfuse dir, name, files = []
  return if files.blank?
  FileUtils.mkpath dir
#      contrast_weight = 0.6
#      entropy_weight = 0.4
#      exposure_weight = 0.5
#      saturation_weight = 0.2
  target = File.join(dir, name + ".tif")
  input = files.map {|file| file.jpg_path }.join(" ")
  #options = "--contrast-weight=#{contrast_weight} --entropy-weight=#{entropy_weight} --exposure-weight=#{exposure_weight} --saturation-weight=#{saturation_weight} --compression=LZW"
  options = "--compression=LZW"
  system "enfuse #{options} -o #{target} #{input}"

  target
end

#pngs(dir, name, file) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/pano/image_file_set.rb', line 86

def pngs dir, name, file
  FileUtils.mkpath dir
  target = File.join(dir, name + ".png").to_s
  cmd = "convert #{file.jpg_path} #{target}"

  puts "-----------------"
  puts cmd
  puts "-----------------"
  system cmd
end

#spit_and_copy_files(apply_mask = false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pano/image_file_set.rb', line 48

def spit_and_copy_files apply_mask = false
  puts "analysing files..."
  panos, misc = split_images
  panos.each_with_index do |pano, index|
    pano_dir = File.join(@base_path, "pano.#{index}")
    puts "coping to #{pano_dir}"
    pano.each do |file|
      file.copy_to pano_dir
    end
    i = 0
    pano.each_slice(BRACKETS) do |files|
      i+=1
      pngs(File.join(pano_dir, "png"), "%02d" % i, files.first)
      fused = enfuse(File.join(pano_dir, "fused"), "%02d" % i, files)
      if apply_mask
        if (1..6).include? i
          system "convert #{fused} #{TOOL_ROOT}/lib/mask.png \
                    +matte -compose CopyOpacity -composite \
                    #{fused}"
        elsif i >= pano.length
          system "convert #{fused} #{TOOL_ROOT}/lib/mask_last.png \
                    +matte -compose CopyOpacity -composite \
                    #{fused}"
        end
      end
      system "mogrift -rotate -90 #{fused}"
      system "exiftool -overwrite_original -TagsFromFile #{files.first.jpg_path} #{fused}"
    end
  end
  
  misc_dir = File.join(@base_path, "misc")
  puts "coping to #{misc_dir}"
  misc.each do |file|
    file.copy_to misc_dir
  end
  
end

#split_imagesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pano/image_file_set.rb', line 15

def split_images
  pano = []
  panos = []
  misc = []
  i = 0
  while i < @files.length 
    file = @files[i]
    if file.for_pano?
      if pano.length < 13 * BRACKETS
        pano << file
      else
        last = pano.last
        if (file.created_at - last.created_at) > 1.5.minutes
          panos << pano
          pano = []
        end
        pano << file
      end
    else
      if pano.present?
        panos << pano
        pano = []
      end
      misc << file
    end
    i+=1
  end
  if pano.present?
    panos << pano
  end
  [panos, misc]
end