class ImageParadise::ImageManipulations

Public Class Methods

gm(file) click to toggle source
#

ImageManipulations.gm

#
# File lib/image_paradise/gm_support.rb, line 28
def self.gm(file)
  instance = new(file)
  instance.extend(GmSupport)
  instance
end
new(i) click to toggle source
#

initialize

The first argument to this method should be the path to the file that we want to manipulate.

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 19
def initialize(i)
  reset
  set_file(i)
end

Public Instance Methods

convert(output, i = {}) click to toggle source
#

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_arguments(i)
convert_to_command(tokens) click to toggle source
#

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() click to toggle source
#

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
Also aliased as: width_and_height
do_manipulate(i = {}) click to toggle source
#

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
Also aliased as: manipulate!
expand_arguments_to_key_value_pairs(i) click to toggle source
#

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
Also aliased as: convert_arguments
file()
Alias for: file?
file?() click to toggle source
#

file?

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 42
def file?
  @file
end
Also aliased as: file
filename_changed?() click to toggle source
#

filename_changed?

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 117
def filename_changed?
  @filename_changed
end
find_file(path, format, layer) click to toggle source
#

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
height() click to toggle source
#

height

Returns the y dimension of an image as an integer

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 93
def height
  dimensions[:y]
end
identify(i = {}) click to toggle source
#

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
manipulate!(i = {})
Alias for: do_manipulate
montage(sources, i = {}) click to toggle source
#

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(format, layer) click to toggle source
#

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
reset() click to toggle source
#

reset

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 34
def reset
  @filename_changed = false
  @display_sys_command = false
end
run_command(i) click to toggle source
#

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
set_file(i) click to toggle source
#

set_file

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 27
def set_file(i)
  @file = i
end
shrink(n_percent = '80%') click to toggle source
#

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
width() click to toggle source
#

width

Returns the x dimension of an image as an integer

#
# File lib/image_paradise/utility_scripts/image_manipulations.rb, line 84
def width
  dimensions[:x]
end
width_and_height()
Alias for: dimensions