require 'RMagick' module Mork # The class Mimage is a wrapper for the core image library, currently RMagick class Mimage def initialize(img, page=0) if img.class == String if File.extname(img) == '.pdf' @image = Magick::Image.read(img) { self.density = 200 }[page] else @image = Magick::ImageList.new(img)[page] end elsif img.class == Magick::ImageList @image = img[page] elsif img.class == Magick::Image @image = img else raise "Invalid initialization argument" end end # ============= # = Highlight = # ============= def highlight!(c) m = Magick::Image.new(c[:w], c[:h]) { self.background_color = "yellow" } @image.composite! m, c[:x], c[:y], Magick::CopyYellowCompositeOp end def join!(p) poly = Magick::Draw.new poly.fill_opacity 0 poly.stroke 'green' poly.stroke_width 3 poly.polygon p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7] poly.draw @image end # ============ # = Cropping = # ============ def crop(c) Mimage.new @image.crop(c[:x], c[:y], c[:w], c[:h]) end def crop!(c) @image.crop!(c[:x], c[:y], c[:w], c[:h]) self end # ============ # = Blurring = # ============ def blur(a, b) Mimage.new @image.blur_image(a, b) end def blur!(a, b) @image = @image.blur_image(a, b) self end # ============== # = Stretching = # ============== def stretch(points) Mimage.new @image.distort(Magick::PerspectiveDistortion, points) end def stretch!(points) @image = @image.distort(Magick::PerspectiveDistortion, points) self end # returns the raw pixels from the entire image or from the area # defined in opts def pixels(opts = {}) c = {x: 0, y: 0, w: width, h: height}.merge(opts) @image.export_pixels(c[:x], c[:y], c[:w], c[:h], "I") end def width @image.columns end def height @image.rows end # write the underlying Magick::Image to disk def write(fname) @image.write fname end end end