Created
July 10, 2009 05:10
-
-
Save tatey/144250 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mini Magick VS Image Science | |
require 'benchmark' | |
require 'rubygems' | |
require 'mini_magick' | |
require 'image_science' | |
@count = 0 | |
def create_mini_magick_thumbnail | |
img = MiniMagick::Image.from_file(ARGV[0]) | |
return if img[:width] < 64 or img[:height] < 64 | |
if img[:width] > img[:height] | |
# Height given, width automatically selected to preserve aspect ratio | |
img.resize('x64') | |
else | |
# Width given, height automatically selected to preserve aspect ratio | |
img.resize('64') | |
end | |
# Format to JPEG before cropping. ImageMagick raises a strange exception | |
# when cropping animated GIFs | |
img.format('jpeg') | |
# Calculate centred offset for best results and crop | |
x_offset = img[:width] > 64 ? (img[:width] - 64) / 2 : 0 | |
y_offset = img[:height] > 64 ? (img[:height] - 64) / 2 : 0 | |
img.crop("64x64+#{x_offset}+#{y_offset}") | |
img.write("dump/#{@count += 1}_image_magick.jpg") | |
end | |
def create_image_science_thumbnail | |
ImageScience.with_image(ARGV[0]) do |img| | |
img.cropped_thumbnail(64) do |thumb| | |
thumb.save("dump/#{@count += 1}_image_science.jpg") | |
end | |
end | |
end | |
Benchmark.benchmark do |b| | |
b.report { 50.times { create_mini_magick_thumbnail } } | |
b.report { 50.times { create_image_science_thumbnail } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment