Module: MpoTools

Defined in:
lib/mpo_tools.rb,
lib/version.rb

Overview

The core library. This module provides methods for converting and extracing data from MPO format 3D photographs. It uses imagemagick and exiftool to do this, so both libraries must be installed on your system for this code to work

Constant Summary collapse

VERSION =
'0.0.1'
FORMATS =

List of supported output formats

[:stereo, :cross_eyed, :wiggle, :analglyph]

Class Method Summary collapse

Class Method Details

.convert(source, destination = nil, format = FORMATS.first, scale = 1) ⇒ Object

Convert the provided MPO file into the desired format

Parameters

source

The location of disk of the .mpo file

destination

The desired output location of the new file (Optional)

format

The desired output format. A list of valid formats can be found in

the +FORMATS+ constant (Optional, defaults to :stereo)
scale

The size of the outputted image. This can be provided as a scale

with 1 representing the original size. Alternatively an array can be
passed in with the format [x_size, y_size]

Returns

  • The location on disk of the newly created output file.

Raises

  • MpoError - Raised when the provided source file cannot be converted



29
30
31
32
33
34
35
# File 'lib/mpo_tools.rb', line 29

def self.convert(source, destination=nil, format=FORMATS.first, scale=1)
  raise MpoError.new "Invalid format: #{format}" if !FORMATS.include?(format)
  source, destination = validate_paths source, destination, format
  left, right = extract_images source
  left, right = scale_images left, right, scale unless scale == 1
  send "output_as_#{format}", left, right, destination
end

.exif_data(source) ⇒ Object

Reads the source MPO file and returns a hash of exif data

Parameters

source

The location on disk of the .mpo file

Returns

  • A ruby hash containing the file’s exif meta data

Raises

  • MpoError - Raised when exif data cannot be read from the source or when

    the exiftool application has not been installed
    


47
48
49
50
51
52
53
54
# File 'lib/mpo_tools.rb', line 47

def self.exif_data(source)
  exif = `exiftool -u -d "%Y-%m-%d %H:%M:%S" -json #{source}`
  return JSON.parse(exif).first
rescue Errno::ENOENT => e
  raise MpoError.new "Please install 'exiftool' on your machine.\n  \"sudo apt-get install libimage-exiftool-perl\" on Ubuntu"
rescue JSON::ParserError => e
  raise MpoError.new 'Unable to read exif data'
end

.extract_images(source) ⇒ Object

Extracts two images from the MPO file

Parameters

source

The location on disk of the .mpo file

Returns

  • The left eye image in Magick::Image format

  • The right eye image in Magick::Image format

Raises

  • MpoError - Raised when two images cannot be created based on the provided

    source file or when the exiftool application has not been installed
    


67
68
69
70
71
72
73
74
75
# File 'lib/mpo_tools.rb', line 67

def self.extract_images(source)
  left = Magick::Image.from_blob(`exiftool -trailer:all= #{source} -o -`)[0]
  right = Magick::Image.from_blob(`exiftool #{source} -mpimage2 -b`)[0]
  return left, right
rescue Errno::ENOENT => e
  raise MpoError.new "Please install 'exiftool' on your machine.\n  \"sudo apt-get install libimage-exiftool-perl\" on Ubuntu"
rescue Magick::ImageMagickError => e
  raise MpoError.new 'Unable to extract images'
end