概要
RubyでJPG入るのExifを扱って表示してみる
きっかけ
毎回インスペクタを見たり、ツールを使って1ファイルずつ見るのがめんどう。ブログに書くときにだいたい決まった形式で表示したい。
方法
gemのexifrを使うと簡単に取れそう
https://github.com/remvee/exifr/
手順
インストール
# console
$ gem install exifr --no-ri --no-rdoc
Fetching: exifr-1.3.5.gem (100%)
Successfully installed exifr-1.3.5
1 gem installed
実行
# pry console
> require 'exifr/jpeg'
=> true
> exif = EXIFR::JPEG.new('/Users/ryo/Downloads/Photos/DSC04117.jpg')
=> #<EXIFR::JPEG:0x00007fe3725b3
# exifが存在するか
> exif.exif?
=> true
# データ構造
> exif.exif
=> [{:image_description=>" ", :make=>"SONY", :model=>"ILCE-7M3", :orientation=>#<EXIFR::TIFF::Orientation:TopLeft(1)>, :x_resolution=>(350/1), :y_resolution=>(350/1), :resolution_unit=>2, :software=>"ILCE-7M3 v1.00", :date_time=>2018-12-19 14:52:08 +0900, :ycb_cr_positioning=>2, :exposure_time=>(1/200), :f_number=>(9/5), :exposure_program=>1, :iso_speed_ratings=>320, :date_time_original=>2018-12-19 14:52:08 +0900, :date_time_digitized=>2018-12-19 14:52:08 +0900, :compressed_bits_per_pixel=>(4/1), :brightness_value=>(4757/1280), :exposure_bias_value=>(0/1), :max_aperture_value=>(217/128), :metering_mode=>2, :light_source=>0, :flash=>16, :focal_length=>(85/1), :user_comment=>"", :color_space=>1, :pixel_x_dimension=>6000, :pixel_y_dimension=>4000, :custom_rendered=>0, :exposure_mode=>1, :white_balance=>0, :digital_zoom_ratio=>(1/1), :focal_length_in_35mm_film=>85, :scene_capture_type=>0, :contrast=>0, :saturation=>0, :sharpness=>0, :lens_model=>"FE 85mm F1.8"}, {:compression=>6, :image_description=>" ", :make=>"SONY", :model=>"ILCE-7M3", :orientation=>#<EXIFR::TIFF::Orientation:TopLeft(1)>, :x_resolution=>(72/1), :y_resolution=>(72/1), :resolution_unit=>2, :software=>"ILCE-7M3 v1.00", :date_time=>2018-12-19 14:52:08 +0900, :jpeg_interchange_format=>38912, :jpeg_interchange_format_length=>10917, :ycb_cr_positioning=>2}]
# 各種データ
> exif.width
=> 6000
> exif.height
=> 4000[19] pry(main)> exif.model
=> "ILCE-7M3"
> exif.lens_model
=> "FE 85mm F1.8"
> exif.f_number
=> (9/5)
> exif.f_number.to_f
=> 1.8
> exif.exposure_time
=> (1/200)
> exif.iso_speed_ratings
=> 320
めっちゃ簡単にデータ取れるじゃん。最高。