22

I am using Rails paperclip for displaying the images in my page. I want to know how to retrieve EXIF information of an image(like dimensions, camera model,height,width., etc).Can any one help me out???

Thanks!!!

Strelok
  • 50,229
  • 9
  • 102
  • 115

4 Answers4

29

Did you give exifr gem a try? From the documentation

EXIFR::JPEG.new('IMG_6841.JPG').width               # => 2272
EXIFR::JPEG.new('IMG_6841.JPG').height              # => 1704
EXIFR::JPEG.new('IMG_6841.JPG').exif?               # => true
EXIFR::JPEG.new('IMG_6841.JPG').model               # => "Canon PowerShot G3"
EXIFR::JPEG.new('IMG_6841.JPG').date_time           # => Fri Feb 09 16:48:54 +0100 2007
EXIFR::JPEG.new('IMG_6841.JPG').exposure_time.to_s  # => "1/15"
EXIFR::JPEG.new('IMG_6841.JPG').f_number.to_f       # => 2.0
Kulbir Saini
  • 3,926
  • 1
  • 26
  • 34
10

There are 3 gems to do this:

  1. mini_exiftool: ExifTool command-line wrapper
  2. exifr: Pure Ruby
  3. exif: C Extension (by me)

If you want to write or edit EXIF tag, you should choose mini_exiftool, it's more powerful but very slow, as the benchmark shown below, exif is 8 times faster than exifr, and 1200 times than that of mini_exiftool.

benchmark:

require 'benchmark'
require 'mini_exiftool'
require 'exifr'
require 'exif'

N = 50
FILE_PATH = File.expand_path('../../spec/sample.jpg', __FILE__)
Benchmark.bmbm do |x|
  x.report 'mini_exiftool' do
    N.times{ MiniExiftool.new(FILE_PATH).image_width }
  end
  x.report 'exifr' do
    N.times{ EXIFR::JPEG.new(FILE_PATH).width }
  end
  x.report 'exif' do
    N.times{ Exif::Data.new(FILE_PATH).image_width }
  end
end

output:

Rehearsal -------------------------------------------------
mini_exiftool   0.150000   0.050000  12.390000 ( 12.546417)
exifr           0.090000   0.000000   0.090000 (  0.091090)
exif            0.010000   0.000000   0.010000 (  0.010343)
--------------------------------------- total: 12.490000sec
                    user     system      total        real
mini_exiftool   0.150000   0.050000  12.400000 ( 12.540122)
exifr           0.080000   0.000000   0.080000 (  0.083251)
exif            0.010000   0.000000   0.010000 (  0.009855)

mini_exiftool is a bit overkill to only retrieve data. So in your case, I think you should use exifr in JRuby, or give exif a try in MRI.

Weihang Jian
  • 7,826
  • 4
  • 44
  • 55
  • Great job with including benchmarks. You should also include what commands were run to generate these benchmarks, otherwise, it's impossible to validate. ;) – Joshua Pinter Jan 30 '17 at 16:27
  • 1
    @JoshPinter Now it's included. Thanks for your reminding. =D – Weihang Jian Jan 30 '17 at 17:58
  • Good job. Thanks! – Joshua Pinter Jan 30 '17 at 20:53
  • The benchmarks look good, but [ruby toolbox](https://www.ruby-toolbox.com/projects/exif) shows that there are a lot of open issues and it hasn't had a release in 3+ years. [Compared to other options](https://www.ruby-toolbox.com/search?q=exif), it's not looking favourable – JellicleCat Apr 16 '21 at 20:49
7

You might try the mini_exiftool gem, a wrapper for the exiftool command-line tool.

While the gem requires you to actually install the command-line tool, the result is that you get a lot more power. Compared to exifr, which only gives you support for JPG and TIF files, exiftool supports a huge number of file formats. It also supports reading and writing of exif data, whereas exifr only supports reading.

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
1
after_photo_post_process :copy_exif_data 
  private 
  def copy_exif_data 
    exif =EXIFR::JPEG.new(photo.queued_for_write[:original]) 
    self.exif_value = exif
  end 

This is working code which i am used.