Class: RMagickTransformationScript
- Inherits:
-
Object
- Object
- RMagickTransformationScript
- Defined in:
- lib/asset_compiler/rmagick_transformation_script.rb
Overview
Holds a reference to an instance of Magick::Image and manages operations on it. Used to provide a more consistent, higher-level api than is given by RMagick.
ImageTransformationScript automatically invokes Ruby’s garbage collector by calling GC.start upon instantiating a Magick::Image object. To bypass this, set the GC environment variable to ‘no’.
Constant Summary collapse
- GRAVITY_TYPES =
{ :none => Magick::ForgetGravity, :northwest => Magick::NorthWestGravity, :north => Magick::NorthGravity, :northeast => Magick::NorthEastGravity, :west => Magick::WestGravity, :center => Magick::CenterGravity, :east => Magick::EastGravity, :southwest => Magick::SouthWestGravity, :south => Magick::SouthGravity, :southeast => Magick::SouthEastGravity }
Instance Method Summary collapse
-
#crop_to(size_string, gravity = :center) ⇒ Object
Resizes the image proportionally to fit the smaller dimension, then crops excess.
-
#darken(factor = 0.25) ⇒ Object
Darkens the image.
-
#greyscale(overlay = '#aa9955') ⇒ Object
Converts the image to greyscale.
-
#icc_profile(path) ⇒ Object
Applies an ICC profile to the image.
-
#icm_profile(path) ⇒ Object
Applies an ICM profile to the image.
-
#initialize(img, &block) ⇒ RMagickTransformationScript
constructor
Initialize an
ImageTransformationScriptwhereimgis either a path to an image or an instance ofMagick::Image. -
#lighten(factor = 0.25) ⇒ Object
Lightens the image.
-
#read(src) ⇒ Object
Read an image from a file specified by
srcand return aMagick::Imageinstance. -
#size_to_fit(dimensions) ⇒ Object
Resizes the image to fit within the constraints specified by
dimensionswheredimensionsis aStringin the format of ‘WIDTHxLENGTH’. -
#tint(r, g, b, overlay) ⇒ Object
Tints the image according to the following parameters: *
rBlending factor for the red channel *gBlending factor for the green channel *bBlending factor for the blue channel *overlayColor to tint the image img.tint 0.25, 0.25, 0.25, #ffffff # lightens the image 25%. -
#unsharp_mask(rad = 0.5, sig = 1.0, blend = 0.5, thresh = 0.25) ⇒ Object
Sharpen only the edges of the image.
-
#write(target) ⇒ Object
Write the image to a specified path,
target.
Constructor Details
#initialize(img, &block) ⇒ RMagickTransformationScript
Initialize an ImageTransformationScript where img is either a path to an image or an instance of Magick::Image.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 25 def initialize img, &block @image = img if img.is_a? Magick::Image @image = read img if img.is_a? String raise ArgumentError.new( 'img must be either a path to an image or a Magick::Image object' ) \ unless @image instance_eval &block GC.start unless ENV['GC'] == 'no' end |
Instance Method Details
#crop_to(size_string, gravity = :center) ⇒ Object
Resizes the image proportionally to fit the smaller dimension, then crops excess. Optionally, a gravity setting may be provided. From the RMagick docs:
Gravity provides a convenient way to locate objects irrespective of the size of the
bounding region, in other words, you don't need to provide absolute coordinates in
order to position an object.
The default gravity setting is :center. Available settings are:
:none-
don’t use gravity
:northwest-
crop to northwest
:north-
crop to north - good for vertical images
:northeast-
crop to northeast
:west-
crop to west
:center-
crop to center - default
:east-
crop to east
:southwest-
crop to southwest
:south-
crop to south
:southeast-
crop to southeast
Usage:
img.crop_to '64x64' # crops to center, 64x64 pixels
img.crop_to '64x64', :north # crops to north - common for thumbnailing portraits
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 127 def crop_to size_string, gravity = :center width, height = size_string.scan( /\d+/ ).collect { |i| i.to_i } max = ( width > height ? width : height ) aspect = @image.columns.to_f / @image.rows.to_f @image.resize!( max, ( max / aspect ).to_i ) if aspect < 1.0 @image.resize!( (max*aspect).to_i, max ) unless aspect < 1.0 # radius, sigma, blend, threshold unsharp_mask 0.5, 1.0, 0.5, 0.25 @image.crop!( GRAVITY_TYPES[gravity], width, height ) @image end |
#darken(factor = 0.25) ⇒ Object
Darkens the image. Defaults to a blend factor of 25%.
img.darken # darkens the image 25%.
img.darken 0.40 # darkens the image 40%.
80 81 82 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 80 def darken factor = 0.25 tint factor, factor, factor, '#000000' end |
#greyscale(overlay = '#aa9955') ⇒ Object
Converts the image to greyscale.
img.greyscale # uses default overlay of #aa9955.
img.greyscale '#aaaaaa' # uses specified overlay.
87 88 89 90 91 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 87 def greyscale = '#aa9955' @image = @image.quantize 256, Magick::GRAYColorspace tint 0.25, 0.25, 0.25, @image end |
#icc_profile(path) ⇒ Object
Applies an ICC profile to the image. Requires path which is a path to a file containing a valid ICC profile.
img.icc_profile 'srgb.icc'
142 143 144 145 146 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 142 def icc_profile path profile = File.open( path ).readlines.join( '' ) if File.exist? path @image.profile! 'ICC', profile if profile @image end |
#icm_profile(path) ⇒ Object
Applies an ICM profile to the image. Requires path which is a path to a file containing a valid ICM profile.
img.icm_profile 'srgb.icm'
151 152 153 154 155 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 151 def icm_profile path profile = File.open( path ).readlines.join('') if File.exist? path @image.profile! 'ICM', profile if profile @image end |
#lighten(factor = 0.25) ⇒ Object
Lightens the image. Defaults to a blend factor of 25%.
img.lighten # lightens the image 25%.
img.lighten 0.40 # lightens the image 40%.
73 74 75 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 73 def lighten factor = 0.25 tint factor, factor, factor, '#ffffff' end |
#read(src) ⇒ Object
Read an image from a file specified by src and return a Magick::Image instance.
37 38 39 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 37 def read src Magick::Image.read( src ).first end |
#size_to_fit(dimensions) ⇒ Object
Resizes the image to fit within the constraints specified by dimensions where dimensions is a String in the format of ‘WIDTHxLENGTH’.
img.size_to_fit '64x64'
96 97 98 99 100 101 102 103 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 96 def size_to_fit dimensions @image.change_geometry dimensions do |w,h,img| img.density = '72x72' img.resize! w, h # radius, sigma, blend, threshold unsharp_mask 0.5, 1.0, 0.5, 0.25 end end |
#tint(r, g, b, overlay) ⇒ Object
Tints the image according to the following parameters:
-
rBlending factor for the red channel -
gBlending factor for the green channel -
bBlending factor for the blue channel -
overlayColor to tint the image img.tint 0.25, 0.25, 0.25, #ffffff # lightens the image 25%
54 55 56 57 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 54 def tint r, g, b, @image = @image.colorize r, g, b, @image end |
#unsharp_mask(rad = 0.5, sig = 1.0, blend = 0.5, thresh = 0.25) ⇒ Object
Sharpen only the edges of the image. Parameters are:
-
radRadius: how big an edge can be -
sigSigma weight for gaussian: leave it at 1.0. -
blendBlending factor: how much sharpening to apply as a percentage -
threshThreshold: how different 2 pixels must be to qualify as an edge img.unsharp_mask 0.5, 1.0, 0.5, 0.25
65 66 67 68 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 65 def unsharp_mask rad = 0.5, sig = 1.0, blend = 0.5, thresh = 0.25 @image = @image.unsharp_mask rad, sig, blend, thresh @image end |
#write(target) ⇒ Object
Write the image to a specified path, target.
42 43 44 45 46 |
# File 'lib/asset_compiler/rmagick_transformation_script.rb', line 42 def write target @image.write target do quality = 95 end end |