Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 139 lines (100 sloc) 4.317 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
= Mini Tutorial


== Installation

* Installing the Exiftool command-line application from Phil Harvey
  (see http://www.sno.phy.queensu.ca/~phil/exiftool/install.html)
* Installing the Ruby library (<code>gem install mini_exiftool</code>)


== Lesson 1: Reading Meta Data

=== A Simple Example

 require 'rubygems'
 require 'mini_exiftool'

 photo = MiniExiftool.new 'photo.jpg'
 puts photo['DateTimeOriginal']


=== Smart Tag Names
In the example above we use <code>photo['DateTimeOriginal']</code> to
get the value for the time the photo was taken. But tag names are not
case sensitive and additional underlines are also irrelevant. So
following expressions are equivalent:
 photo['DateTimeOriginal']
 photo['datetimeoriginal']
 photo['date_time_original']

It is also possible to use symbols:
 photo[:DateTimeOriginal]
 photo[:datetimeoriginal]
 photo[:date_time_original]

=== Nicer Access Via Dynamic Methods

Using the []-method is the safest way to access to values of tags
(e. g. Self-timer you can only access this way) but the smarter way is
using dynamic method access. You can write:
 photo.datetimeoriginal
or also
 photo.date_time_original


=== Value Types

Following types of values are at the moment supported:
* Array (e. g. Keywords => ['tree', 'gras'])
* Fixnum (e. g. ISO => 400)
* Float (e. g. FNumber => 9.5)
* String (e. g. Model => DYNAX 7D)
* Time (e. g. DateTimeOriginal => 2005:09:13 20:08:50)

Be aware, if there is only one value in a tag which can hold multiple
values the result is'nt an array! But you can get one with the to_a
method:
  # only _one_ keyword
  p1 = MiniExiftool.new 'p1.jpg'
  p1.keywords # => 'red'
  # _more than one_ keywords
  p3 = MiniExiftool.new 'p3.jpg'
  p3.keywords # => ['red', 'yellow', 'green']

  # if we want to get an array in both cases and don't know
  # if there is one ore more values set let's take to_a
  p1.keywords.to_a # => ['red']
  p3.keywords.to_a # => ['red', 'yellow', 'green']

The Exiftool command-line application has an option (-n) to get values
as numbers if possible, in MiniExiftool you can do this with setting
the <code>:numerical</code> option to +true+ while generating a new
instance with new or using the <code>numerical=</code>-method
combining with calling <code>reload</code>.

Let's look at an example:
 # standard: numerical is false
 photo = MiniExiftool.new 'photo.jpg'
 photo.exposure_time # => '1/60' (String)
 # now with numerical is true
 photo.numerical = true
 photo.reload
 photo.exposure_time # => 0.01666667 (Float)
This behaviour can be useful if you want to do calculations on the
value, if you only want to show the value the standard behaviour is
maybe better.

The Time class of Ruby cannot handle timestamps before 1st January 1970
on some platforms. If there are timestamps in files before this date it
will result in an error. In this case we can set the option
<code>:timestamps</code> to +DateTime+ to use DateTime objects instead
of Time objects.

There is another option <code>:composite</code>. If this is set to
+false+ the composite tags are not calculated by the exiftool
command-line application (option -e).

=== Further Example

For understanding reading access to meta data also have a look at the
example file <code>print_portraits.rb</code> in the +examples+
directory.

== Lesson 2: Writing Meta Data


=== Also A Very Simple Example

 require 'rubygems'
 require 'mini_exiftool'

 photo = MiniExiftool.new 'photo.jpg'
 photo.comment = 'hello world'
 photo.save


=== Save Is Atomar

If you have changed serval values and call the +save+-method either
all changes will be written to the file or nothing. The return value
of the +save+-method is +true+ if all values are written to the file
otherwise save returns +false+. In the last case you can use the
+errors+-method which returns a hash of the tags which values couldn't
be writed with an error message for each of them.


=== Interesting Methods

Have a look at the <code>changed?</code>-method for checking if the
value of a specific tag is changed or a changing in general is
done. In the same way the +revert+-method reverts the value of a
specific tag or in general all changes.

You should also look at the rdoc information of MiniExiftool.

=== Further Example

See <code>shift_time.rb</code> in the +examples+ directory.
Something went wrong with that request. Please try again.