class ImageParadise::ImageManipulations
Public Class Methods
Public Instance Methods
#¶ ↑
convert (convert tag)¶ ↑
Runs ImageMagick’s ‘convert’.
The second argument to this method has to be a Hash, if it is given.
See www.imagemagick.org/script/convert.php
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 218 def convert(output, i = {}) tokens = ['convert'] tokens << expand_arguments_to_key_value_pairs(i) if i tokens << " '#{@file}#{"[#{i[:layer].to_s}]" if i.has_key?(:layer)}'" tokens << " -annotate #{i[:annotate].to_s}" if i.has_key?(:annotate) tokens << " #{output}" tokens = convert_to_command(tokens) success = run_command(tokens)[1] success end
#¶ ↑
convert_to_command
¶ ↑
This method adds a proper prefix, which is defined in the file gm_support.rb. Then it joins the commands of the Array at hand.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 235 def convert_to_command(tokens) # ======================================================================= # # Pass into the method prefix() next. # ======================================================================= # tokens[0] = prefix(tokens.first) if respond_to? :prefix tokens.flatten.join end
#¶ ↑
dimensions¶ ↑
Return the x and y dimensions of an image, as a Hash.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 67 def dimensions dimensions = identify( # Tap into Imagemagick's identify binary. layer: 0, format: '%wx%h' ).chomp.split('x') hash = { x: dimensions[0].to_i, y: dimensions[1].to_i } return hash end
#¶ ↑
do_manipulate
¶ ↑
Runs ImageMagick’s ‘mogrify’ command. The first argument to this method is an (optional) Hash.
See www.imagemagick.org/script/mogrify.php
Invocation example:
image.do_manipulate(scale: '80%') # => true
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 134 def do_manipulate(i = {}) tokens = ['mogrify'] tokens << expand_arguments_to_key_value_pairs(i) if i tokens << " '#{@file}#{"[#{i[:layer].to_s}]" if i[:layer]}'" tokens << " -annotate #{i[:annotate].to_s}" if i[:annotate] tokens = convert_to_command(tokens) success = run_command(tokens)[1] replace_file(i[:format].to_s.downcase, i[:layer]) if success && i[:format] success end
#¶ ↑
expand_arguments_to_key_value_pairs
¶ ↑
Convert the entries into a format that ImageMagick understands.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 248 def expand_arguments_to_key_value_pairs(i) special_arguments = [ :layer, :annotate ] # ======================================================================= # # First, reject keys that are not :layer or :annotate. # ======================================================================= # i.reject {|key, _value| special_arguments.include?(key) }.map {|key, value| " -#{key} '#{value}'" } end
#¶ ↑
file?¶ ↑
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 42 def file? @file end
#¶ ↑
find_file
¶ ↑
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 192 def find_file(path, format, layer) possible_paths = [ Proc.new { |inner_path, inner_format, _inner_layer| "#{inner_path}.#{inner_format}" }, Proc.new { |inner_path, inner_format, inner_layer| "#{inner_path}-#{inner_layer}.#{format}" }, Proc.new { |inner_path, inner_format, inner_layer| "#{inner_path}.#{inner_format}.#{inner_layer}" } ] possible_paths.find { |possible_path| File.exist?(possible_path.call(path, format, layer)) } end
#¶ ↑
identify¶ ↑
Runs ImageMagick’s ‘identify’.
See www.imagemagick.org/script/identify.php
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 53 def identify(i = {}) tokens = ['identify'] tokens << expand_arguments_to_key_value_pairs(i) if i tokens << " '#{@file}#{"[#{i[:layer].to_s}]" if i[:layer]}'" tokens = convert_to_command(tokens) output = run_command(tokens).first output end
#¶ ↑
montage¶ ↑
Runs ImageMagick’s ‘montage’.
See www.imagemagick.org/script/montage.php
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 104 def montage(sources, i = {}) tokens = ['montage'] tokens << expand_arguments_to_key_value_pairs(i) if i sources.each {|source| tokens << " '#{source}'" } tokens << " '#{@file}'" tokens = convert_to_command(tokens) success = run_command(tokens)[1] success end
#¶ ↑
replace_file
¶ ↑
Replaces the old file (with the old file format) with the newly generated one.
The old file will be deleted and $file will be reset.
If ImageMagick generated more than one file, $file will have a “*”, so that all files generated will be manipulated in the following steps.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 173 def replace_file(format, layer) return if File.extname(@file) == format layer ||= 0 layer = layer.split(',').first if layer.is_a? String File.delete(@file) @filename_changed = true path = File.join File.dirname(@file), File.basename(@file, File.extname(@file)) new_file = find_file(path, format, layer) @file = new_file.call(path, format, '*') unless new_file.nil? end
#¶ ↑
run_command
¶ ↑
Use IO.popen to run the passed command at hand.
The method returns an Array, holding the output, and the success status.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 266 def run_command(i) i = i.to_s if @display_sys_command # ===================================================================== # # Show the command if the ivar above is true. # ===================================================================== # e i end output = IO.popen(i) {|dataset| dataset.read } success = $?.exitstatus == 0 ? true : false [output, success] end
#¶ ↑
shrink¶ ↑
Shrinks the given image at hand. Be careful with this method - it will instantly shrink the given file, without keeping a backup. So if you want to keep a backup, you have to copy the image file on your own BEFORE invoking this method here.
#¶ ↑
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 153 def shrink(n_percent = '80%') unless n_percent.is_a? String n_percent = n_percent.to_s end n_percent << '%' unless n_percent.end_with? '%' do_manipulate(scale: n_percent) end