Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from ethansr/from_hash

from_hash applies type conversion to values
  • Loading branch information...
commit d911d564adab47825a2957f025d6425eb8b4812d 2 parents 36f9351 + 73e8cef
janfri authored
View
18 lib/mini_exiftool.rb
@@ -65,7 +65,7 @@ def initialize filename=nil, opts={}
def initialize_from_hash hash # :nodoc:
hash.each_pair do |tag,value|
- set_value tag, value
+ set_value tag, perform_conversions(value)
end
set_attributes_by_heuristic
self
@@ -198,7 +198,7 @@ def to_yaml
to_hash.to_yaml
end
- # Create a MiniExiftool instance from a hash
+ # Create a MiniExiftool instance from a hash. Default value conversions will be applied if neccesary.
def self.from_hash hash
instance = MiniExiftool.new
instance.initialize_from_hash hash
@@ -325,7 +325,14 @@ def parse_output
def parse_line line
if line =~ /^([^\t]+)\t(.*)$/
- tag, value = $1, $2
+ tag, value = $1, perform_conversions($2)
+ else
+ raise MiniExiftool::Error.new("Malformed line #{line.inspect} of exiftool output.")
+ end
+ return [tag, value]
+ end
+
+ def perform_conversions(value)
case value
when /^\d{4}:\d\d:\d\d \d\d:\d\d:\d\d/
s = value.sub(/^(\d+):(\d+):/, '\1-\2-')
@@ -353,10 +360,7 @@ def parse_line line
when /#{@@separator}/
value = value.split @@separator
end
- else
- raise MiniExiftool::Error.new("Malformed line #{line.inspect} of exiftool output.")
- end
- return [tag, value]
+ value
end
def set_value tag, value
View
106 test/data/test.jpg.json
@@ -0,0 +1,106 @@
+[{
+ "SourceFile": "test/data/test.jpg",
+ "ExifToolVersion": 8.77,
+ "FileName": "test.jpg",
+ "Directory": "test/data",
+ "FileSize": "46 kB",
+ "FileModifyDate": "2012:07:05 20:28:24-07:00",
+ "FilePermissions": "rw-r--r--",
+ "FileType": "JPEG",
+ "MIMEType": "image/jpeg",
+ "JFIFVersion": 1.01,
+ "ExifByteOrder": "Big-endian (Motorola, MM)",
+ "ImageDescription": "KONICA MINOLTA DIGITAL CAMERA",
+ "Make": "KONICA MINOLTA",
+ "Model": "DYNAX 7D",
+ "Orientation": "Horizontal (normal)",
+ "XResolution": 72,
+ "YResolution": 72,
+ "ResolutionUnit": "inches",
+ "Software": "DYNAX 7D v1.10",
+ "ModifyDate": "2005:09:13 20:08:50",
+ "YCbCrPositioning": "Centered",
+ "ExposureTime": "1/60",
+ "FNumber": 9.5,
+ "ExposureProgram": "Program AE",
+ "ISO": 400,
+ "ExifVersion": "0221",
+ "DateTimeOriginal": "2005:09:13 20:08:50",
+ "CreateDate": "2005:09:13 20:08:50",
+ "ComponentsConfiguration": "Y, Cb, Cr, -",
+ "BrightnessValue": 4.5,
+ "ExposureCompensation": -1,
+ "MaxApertureValue": 4.5,
+ "MeteringMode": "Multi-segment",
+ "LightSource": "Unknown",
+ "Flash": "Off, Did not fire",
+ "FocalLength": "75.0 mm",
+ "SubjectArea": "1504 1000 256 304",
+ "MakerNoteVersion": "MLT0",
+ "MinoltaImageSize": "Large",
+ "WhiteBalance": "Auto",
+ "FocusMode": "AF-A",
+ "AFPoints": "Center",
+ "FlashMode": "Normal",
+ "ISOSetting": 400,
+ "FreeMemoryCardImages": 202,
+ "HueAdjustment": 0,
+ "Rotation": "Horizontal (normal)",
+ "ImageNumber": 6,
+ "NoiseReduction": "Unknown (2)",
+ "ImageNumber2": 50,
+ "ZoneMatchingOn": "Off",
+ "CompressedImageSize": 1598477,
+ "PreviewImageStart": 39152,
+ "PreviewImageLength": 0,
+ "SceneMode": "Standard",
+ "ColorMode": "Natural sRGB",
+ "MinoltaQuality": "Fine",
+ "FlashExposureComp": 0,
+ "Teleconverter": "None",
+ "ImageStabilization": "On",
+ "ZoneMatching": "ISO Setting Used",
+ "ColorTemperature": 0,
+ "LensType": "Minolta AF 28-135mm F4-4.5 or Sigma Lens",
+ "UserComment": "",
+ "FlashpixVersion": "0100",
+ "ColorSpace": "sRGB",
+ "ExifImageWidth": 3008,
+ "ExifImageHeight": 2000,
+ "CustomRendered": "Normal",
+ "ExposureMode": "Auto",
+ "DigitalZoomRatio": 0,
+ "FocalLengthIn35mmFormat": "112 mm",
+ "SceneCaptureType": "Standard",
+ "GainControl": "Low gain up",
+ "Contrast": "Normal",
+ "Saturation": "Normal",
+ "Sharpness": "Normal",
+ "PrintIMVersion": "0300",
+ "Compression": "JPEG (old-style)",
+ "ThumbnailOffset": 39274,
+ "ThumbnailLength": 1820,
+ "CurrentIPTCDigest": "dd8d51d28ddf04f08f870e5ff2f64d01",
+ "Keywords": ["Orange","Rot"],
+ "ApplicationRecordVersion": 4,
+ "SupplementalCategories": "Natur",
+ "XMPToolkit": "Image::ExifTool 7.03",
+ "Title": "Abenddämmerung",
+ "ImageWidth": 300,
+ "ImageHeight": 199,
+ "EncodingProcess": "Baseline DCT, Huffman coding",
+ "BitsPerSample": 8,
+ "ColorComponents": 3,
+ "YCbCrSubSampling": "YCbCr4:2:0 (2 2)",
+ "Aperture": 9.5,
+ "ImageSize": "300x199",
+ "LensID": "Minolta AF 28-135mm F4-4.5",
+ "ScaleFactor35efl": 1.5,
+ "ShutterSpeed": "1/60",
+ "ThumbnailImage": "(Binary data 1820 bytes)",
+ "CircleOfConfusion": "0.020 mm",
+ "FOV": "18.3 deg",
+ "FocalLength35efl": "75.0 mm (35 mm equivalent: 112.0 mm)",
+ "HyperfocalDistance": "29.43 m",
+ "LightValue": 10.4
+}]
View
22 test/test_from_hash.rb
@@ -0,0 +1,22 @@
+require 'helpers_for_test'
+require 'json'
+
+class TestFromHash < TestCase
+ def setup
+ @data_dir = File.dirname(__FILE__) + '/data'
+ hash_data = JSON.parse(File.read( @data_dir + '/test.jpg.json')).first
+ @mini_exiftool = MiniExifTool.from_hash hash_data
+ end
+
+ def test_conversion
+ assert_kind_of String, @mini_exiftool.model
+ assert_kind_of Time, @mini_exiftool['DateTimeOriginal']
+ assert_kind_of Float, @mini_exiftool['MaxApertureValue']
+ assert_kind_of String, @mini_exiftool.flash
+ assert_kind_of Fixnum, @mini_exiftool['ExposureCompensation']
+ assert_kind_of String, (@mini_exiftool['SubjectLocation'] || @mini_exiftool['SubjectArea'])
+ assert_kind_of Array, @mini_exiftool['Keywords']
+ assert_kind_of String, @mini_exiftool['SupplementalCategories']
+ assert_kind_of Rational, @mini_exiftool.shutterspeed
+ end
+end

0 comments on commit d911d56

Please sign in to comment.
Something went wrong with that request. Please try again.