Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This gets called as Image.from("directory"){|i| some_code}
- def self.scan(directory, debug = false, &block)
- puts block.inspect if block
- return nil unless File.directory?(directory)
- tens = 0; count = 0
- Dir.entries(directory).map do |file|
- if count == 10
- tens += 1; count = 0
- print "."; STDOUT.flush
- end
- next if [".", ".."].include?(file) or File.directory?(file)
- if block_given?
- i = Image.ping("#{directory}/#{file}") rescue next
- # next unless yield i.first
- next unless block.call(i.first).call
- else
- Image.ping("#{directory}/#{file}") rescue next
- end
- # This might be handy in large directories...
- GC.start
- count += 1 if debug
- "#{directory}/#{file}"
- end.compact
- end
- def self.from(collection, &block)
- if collection.is_a?(String)
- collection = (block) ? scan(collection) { block } : scan(collection)
- end
- puts collection.inspect
- i = Image.read(collection.random)
- return i if i.size > 1
- i.first
- end
Add Comment
Please, Sign In to add comment