Skip to content

Instantly share code, notes, and snippets.

@kenchan
Created October 16, 2014 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenchan/c563a9a24fc58db01564 to your computer and use it in GitHub Desktop.
Save kenchan/c563a9a24fc58db01564 to your computer and use it in GitHub Desktop.
imlib2 imagemagick benchamrk
require 'mini_magick'
require 'imlib2'
require 'benchmark/ips'
require 'fileutils'
require 'tapp'
THUMB_SIZE = 80
SRC_DIR = Pathname('/Users/kenchan/Dropbox/Camera Uploads')
DEST_DIR = Pathname.pwd.join('tmp')
def imlib2(filepath)
image = Imlib2::Image.load(filepath.to_s)
width = image.width
height = image.height
if width > height
size = height
start_x = (width - height) / 2
start_y = 0
else
size = width
start_x = 0
start_y = (height - width) / 2
end
dest= image.create_cropped_scaled(start_x, start_y, size, size, THUMB_SIZE, THUMB_SIZE)
dest['quality'] = 100
dest['compression'] = 0
dest.save(DEST_DIR.join(filepath.basename).to_s)
end
def imagemagick(filepath)
MiniMagick::Tool::Convert.new do |c|
c.repage.+
c.strip
c.resize '80x80^'
c.quality 100
c.gravity 'center'
c.crop '80x80+0+0'
c << filepath
c << DEST_DIR.join(filepath.basename).to_s
end
end
def imagemagick_define(filepath)
MiniMagick::Tool::Convert.new do |c|
c.repage.+
c.strip
c.define 'jpeg:size=80x80'
c.resize '80x80^'
c.quality 100
c.gravity 'center'
c.crop '80x80+0+0'
c << filepath
c << DEST_DIR.join(filepath.basename).to_s
end
end
FileUtils.mkdir_p(DEST_DIR)
Benchmark.ips do |x|
%w(imlib2 imagemagick imagemagick_define).each do |lib|
x.report(lib) do
Pathname.glob(SRC_DIR.join("*.jp*g")).first(100).each do |f|
send(lib, f)
end
end
end
end
%w(imlib2 imagemagick imagemagick_define).each do |lib|
Benchmark.bm do |x|
x.report(lib) do
Pathname.glob(SRC_DIR.join("*.jp*g")).first(10).each do |f|
send(lib, f)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment