Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
fill_dropped_frames.rb
#!/usr/bin/env ruby
require 'fileutils'
# Takes two arguments:
# Arg 1 - path to the "GRP/frame information" txt file, output by LadybugCapPro
# Arg 2 - path to the folder containing all the fames as PNG files. Assumes prefix 'ladybug_panoramic_'
path_to_frame_info_file = ARGV[0]
path_to_png_folder = ARGV[1]
# find all frames that need to be filled in
dropped_frames = []
prev_line = ""
File.readlines(path_to_frame_info_file).each do |line|
num_dropped_frames_match = /ERROR: Dropped frame : (\d)/.match(line)
frame_number_match = /^(\d{6})/.match(prev_line)
prev_line = line
next unless num_dropped_frames_match && frame_number_match
dropped_frames.push({num: num_dropped_frames_match[1].to_i, frame: frame_number_match[1].to_i})
end
FileUtils.cd(path_to_png_folder) do
# move all files to new naming convention so they don't get overwritten while filing in gaps
Dir['*.png'].each do |filename|
FileUtils.mv filename, "#{filename}.tmp"
end
# put files back in sequence, duplicating neighbouring frames to fill gaps
extra_frames = 0
Dir['*.png.tmp'].each do |filename|
number = /ladybug_panoramic_(\d{6}).png.tmp/.match(filename)[1].to_i
new_name = "ladybug_panoramic_#{(number + extra_frames).to_s.rjust(6, '0')}.png"
puts "Frame #{filename} is now file #{new_name}"
FileUtils.mv filename, new_name
dropped = dropped_frames.find {|frame| frame[:frame] == number}
next unless dropped
puts "Copying frame #{new_name} #{dropped[:num]} times"
(1..dropped[:num]).each do |n|
extra_frames += 1
FileUtils.cp new_name, "ladybug_panoramic_#{(number + extra_frames).to_s.rjust(6, '0')}.png"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment