Skip to content

Commit

Permalink
add rake task to re-create polaroid images, fix for imagemagick 7
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom committed Jul 2, 2018
1 parent e5c2559 commit 96c204b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
38 changes: 21 additions & 17 deletions app/models/picture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def all
File.basename(File.dirname(animation))
end
Rails.logger.warn "No picture sets found at: #{PICTURE_PATH}" if dirs.empty?
dirs.sort.reverse.map { |dir| PictureSet.new(date: dir) }
dirs.sort.reverse.map { |dir| new(date: dir) }
end

def find(date)
Expand All @@ -31,7 +31,7 @@ def create(date: Time.now.getlocal.strftime(DATE_FORMAT))
picture_set = PictureSet.new(date: date)
FileUtils.mkdir(picture_set.dir)
angle = Random.rand(353..366)
jobs = (1..4).collect { |i| capture_job(i, date, picture_set.dir, angle) }
jobs = (1..4).collect { |i| capture_job(i, picture_set, angle) }
GpioPort.on(GpioPort::GPIO_PORTS['PROCESSING'])
# wait until convert jobs are finished
until jobs.none?(&:status) do end
Expand All @@ -44,12 +44,12 @@ def create(date: Time.now.getlocal.strftime(DATE_FORMAT))

private

def capture_job(num, date, dir, angle)
def capture_job(num, picture_set, angle)
GpioPort.on(GpioPort::GPIO_PORTS["PICTURE#{num}"])
begin
retries ||= 0
Syscall.execute("gphoto2 --capture-image-and-download --filename #{date}_#{num}.jpg", dir: dir)
raise 'Image capture failed' unless File.exist?(File.join(dir, "#{date}_#{num}.jpg"))
Syscall.execute("gphoto2 --capture-image-and-download --filename #{picture_set.date}_#{num}.jpg", dir: picture_set.dir)
raise 'Image capture failed' unless File.exist?(File.join(picture_set.dir, "#{picture_set.date}_#{num}.jpg"))
rescue StandardError => e
# rubocop:disable GuardClause
if (retries += 1) < 3
Expand All @@ -60,20 +60,12 @@ def capture_job(num, date, dir, angle)
end
# rubocop:enable GuardClause
end
convert_thread(num, date, dir, angle)
convert_thread(num, picture_set, angle)
end

def convert_thread(num, date, dir, angle)
caption = OPTS.image_caption || date
def convert_thread(num, picture_set, angle)
t = Thread.new do
Syscall.execute("time convert -caption '#{caption}' #{date}_#{num}.jpg " \
'-sample 600 ' \
'-bordercolor Snow ' \
'-density 100 ' \
'-gravity center ' \
"-pointsize #{OPTS.image_fontsize} " \
"-polaroid -#{angle} " \
"#{date}_#{num}#{POLAROID_SUFFIX}", dir: dir)
picture_set.convert_to_polaroid(num, angle)
end
t.abort_on_exception = true
t
Expand All @@ -94,8 +86,20 @@ def destroy
Syscall.execute("rm -r #{date}", dir: PICTURE_PATH)
end

def convert_to_polaroid(num, angle)
caption = OPTS.image_caption || date
Syscall.execute("time convert -caption '#{caption}' #{date}_#{num}.jpg " \
'-sample 600 ' \
'-bordercolor Snow ' \
'-density 100 ' \
'-gravity center ' \
"-pointsize #{OPTS.image_fontsize} " \
"-polaroid -#{angle} " \
'-trim +repage ' \
"#{date}_#{num}#{POLAROID_SUFFIX}", dir: dir)
end

# Merge all polaroid previews to an animated gif
# TODO: Imagemagick >= 7 creates an empty gif from the polaroid pngs...
def create_animation(overwrite: false)
if File.exist?(File.join(dir, animation)) && !overwrite
Rails.logger.info "Skipping for existing animation #{dir}"
Expand Down
Empty file removed lib/tasks/.keep
Empty file.
11 changes: 10 additions & 1 deletion lib/tasks/picture_set.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ namespace :picture_set do
PictureSet.all.each { |ps| ps.combine_images(overwrite: true) }
end

desc 'Re-create polaroid gifs'
desc 'Re-create single polaroid images'
task :recreate_polaroid_images, [:path] => [:environment] do |_task, args|
PictureSet::PICTURE_PATH = args[:path] if args[:path].present?
PictureSet.all.each do |ps|
angle = Random.rand(355..365)
(1..4).each { |num| ps.convert_to_polaroid(num, angle) }
end
end

desc 'Re-create animated gifs'
task :recreate_animations, [:path] => [:environment] do |_task, args|
PictureSet::PICTURE_PATH = args[:path] if args[:path].present?
PictureSet.all.each { |ps| ps.create_animation(overwrite: true) }
end
Expand Down
Empty file removed public/picture_sets/.gitkeep
Empty file.

Large diffs are not rendered by default.

0 comments on commit 96c204b

Please sign in to comment.