Skip to content

Instantly share code, notes, and snippets.

@JamesHarrison
Created January 7, 2012 17:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JamesHarrison/1575463 to your computer and use it in GitHub Desktop.
Save JamesHarrison/1575463 to your computer and use it in GitHub Desktop.
app = Dragonfly[:images]
app.configure_with(:imagemagick)
app.analyser.add :average_intensity do |temp_object|
(`python lib/simple_analyser.py #{temp_object.path}`.gsub("\n","").strip.to_f rescue 0.0)
end
app.processor.add :first_frame_of_gif_as_png do |temp_object|
tempfile = Tempfile.new(['dragonfly','.png'])
tempfile.binmode
tempfile.close
`convert #{temp_object.path}[0] #{tempfile.path}`
tempfile
end
class Image
include Mongoid::Document
# Storage
field :image_aspect_ratio, type: Float
field :average_intensity, type: Float
# Index
index :image_aspect_ratio
index :average_intensity
# Hook to do the analysis step
after_create :set_average_intensity!
def set_average_intensity!
# Examples of how to handle SVG/GIF stuff here.
if self.image_mime_type.downcase.include?("svg")
self.average_intensity = self.image.png.average_intensity
elsif self.image_mime_type.downcase.include?("gif")
self.average_intensity = self.image.process(:first_frame_of_gif_as_png).average_intensity
else
self.average_intensity = self.image.average_intensity
end
self.save
end
def find_duplicates(dist=0.15)
return Image.where(
:average_intensity.lt=>(self.average_intensity+dist), :average_intensity.gt=>(self.average_intensity-dist),
:image_aspect_ratio.lt=>(self.image_aspect_ratio+dist), :image_aspect_ratio.gt=>(self.image_aspect_ratio-dist)
).not_in(id: [self.id]).all rescue []
end
end
from opencv.cv import *
from opencv.highgui import *
from optparse import OptionParser
class Simple2DAnalyser:
def __init__(self, imagename):
im = cvLoadImageM(imagename,CV_LOAD_IMAGE_COLOR)
cvSmooth(im, im, CV_GAUSSIAN, 3)
sums = cvSum(im)
self.r = (sums[0]/(im.rows*im.cols))*0.7
self.g = (sums[1]/(im.rows*im.cols))*1
self.b = (sums[2]/(im.rows*im.cols))*0.5
def getValues(self):
return ((self.b+self.g+self.r)/3)
usage = "usage: %prog filename"
parser = OptionParser(usage=usage)
(options, args) = parser.parse_args()
s = Simple2DAnalyser(args[0])
print s.getValues()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment