Skip to content

Instantly share code, notes, and snippets.

@kvannotten
Created October 31, 2013 08:06
Show Gist options
  • Save kvannotten/7245861 to your computer and use it in GitHub Desktop.
Save kvannotten/7245861 to your computer and use it in GitHub Desktop.
Script that allows you to run some manipulations on images in a threaded way. This script uses celluloid to create actors that manipulate the pictures by resizing them and adding a border.
require "celluloid"
DIRECTORY = "PUT DIRECTORY HERE"
PICTURE_EXTENSIONS = %w{ jpg png }
BORDER_SIZE = "3x3"
BORDER_COLOR = "black"
NEW_SIZE = 1200
DO_AUTO_ORIENT = false
WORKERS = 8
class PictureManipulator
include Celluloid
def initialize
end
def manipulate(pic)
puts "Start converting #{pic}..."
%x{ convert #{pic} -resize #{NEW_SIZE} #{pic} }
%x{ convert #{pic} -shave #{BORDER_SIZE} -bordercolor #{BORDER_COLOR} -border #{BORDER_SIZE} #{pic} }
puts "Finished converting #{pic}"
end
end
pool = PictureManipulator.pool(size: WORKERS)
futures = []
PICTURE_EXTENSIONS.concat PICTURE_EXTENSIONS.map(&:upcase)
Dir.chdir(DIRECTORY) do
Dir.glob("*.{#{PICTURE_EXTENSIONS.join(",")}}").each do |file|
futures << pool.future.manipulate("#{DIRECTORY}/#{file}")
end
end
futures.map(&:value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment