Created
November 3, 2011 22:39
-
-
Save abuisman/1338131 to your computer and use it in GitHub Desktop.
This file contains 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
#Inside Photo model | |
has_attached_file :file, | |
:processors => [ :blurplates, :thumbnail ], | |
:styles => { :frontpage => "460x100000", :view => "740x100000", :medium => '300x300', :thumb => "100x100>" } | |
#blurplates.rb | |
# Load libs | |
require 'camellia' | |
require 'RMagick' | |
#include RMagick | |
#include Camellia | |
module Paperclip | |
# Handles thumbnailing images that are uploaded. | |
class Blurplates < Processor | |
def initialize(file, options = {}, attachment = nil) | |
# Make the file available to this class | |
@file = file | |
# Make an instance of the attachment available | |
@attachment = attachment.instance | |
# Make the basename of the file available | |
@basename = File.basename(file.path, File.extname(file.path)) | |
end | |
# Make will actually work on the file and then return it | |
def make | |
logger.debug "The wheels on the bus go round and round" | |
# Create a new temporary file to work with | |
dst = Tempfile.new([@basename, 'bmp'].compact.join(".")) | |
dst.binmode | |
# Open up the image in ImageMagick to convert it to a bmp. | |
image = Magick::Image.read(File.expand_path(file.path)) | |
image.format = "BMP" | |
image.write(File.expand_path(dst.path)) | |
# Load image into Camellia | |
carmage = CamImage.new | |
carmage.load_bmp(File.expand_path(dst.path)) | |
yuv = image.to_yuv | |
# consider only the V plane (red) | |
yuv.set_roi(CamROI.new(3,0,0,yuv.width,yuv.height)) | |
# threshold and encode | |
thr=yuv.encode_threshold(150) | |
# labeling | |
blobs=thr.labeling! | |
#puts "#{blobs.nb_blobs} blobs detected" | |
# draw rectangle on all detected blobs | |
blobs.each {|b| carmage.draw_rectangle(b.left,b.top,b.left+b.width-1,b.top+b.height-1,cam_rgb(255,0,0))} | |
# save the resulting picture | |
carmage.save_bmp(File.expand_path(dst.path)) | |
# find out the biggest blob | |
sorted=blobs.sort {|a,b| b.surface<=>a.surface} | |
#puts "The bigger blob is at position (#{sorted[0].cx},#{sorted[0].cy}) and its surface is #{sorted[0].surface}" | |
# Load the image into ImageMagick again in order to convert it to a jpeg. | |
lastImage = Magick::Image.read(File.expand_path(dst.path)) | |
lastImage.format = "JPG" | |
lastImage.write(File.expand_path(dst.path)) | |
dst | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment