LoginSignup
27
21

More than 5 years have passed since last update.

auto_orientってExif情報も書き換えてくれるの?

Posted at

結論

適切に書き換えてくれます。

なんでこんなことを思ったのか

Carrierwaveで画像をアップロードする際、Exif情報にあわせて画像を回転させたいよねというニーズに応えるためにこんなメソッドを用意して、

module CarrierWave
  module MiniMagick
    def fix_exif_rotation
      manipulate! do |img|
        img.auto_orient
        img = yield(img) if block_given?
        img
      end
    end
  end
end

こんな風に使ったりしますよね。

process :fix_exif_rotation

でもauto_orientをやったときって、Exif情報もちゃんと変更されるんですかね? Exif情報がそのままの状態で画像が回転してしまっているといったビミョーな状態にはちゃんとならないようになっているのでしょうか? ちょっと見てみましょう。

auto_orientの中身はimagemagickの-auto-orientオプション

auto_orientでやっている内容といえば、中身はImageMagickの-auto-orientオプションなので、実際にImageMagickでこのオプションをつけて変換してやれば良いことになります。

対象のファイル

こんなExif情報を持つ、Exif情報を加味して画像表示すると、180度回転して表示される画像で試してみます。

(こちらで使っているExifToolの使い方はこちらをご覧ください:テスト用にEXIF情報を加工するときに知っておきたいExifTool

$ exiftool sample.jpg
ExifTool Version Number         : 9.63
File Name                       : sample.jpg
Directory                       : .
File Size                       : 197 kB
File Modification Date/Time     : 2014:06:12 11:16:03+09:00
File Access Date/Time           : 2014:06:12 11:26:24+09:00
File Inode Change Date/Time     : 2014:06:12 11:16:04+09:00
File Permissions                : rw-rw-rw-
File Type                       : JPEG
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : None
X Resolution                    : 1
Y Resolution                    : 1
Exif Byte Order                 : Big-endian (Motorola, MM)
Make                            : SonicGarden
Orientation                     : Rotate 180
Color Space                     : sRGB
Exif Image Width                : 1024
Exif Image Height               : 768
Image Width                     : 1024
Image Height                    : 768
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 1024x768

この画像に対してconvertをかけると・・・

$ convert -auto-orient sample.jpg sample_auto_orient.jpg
$ exiftool -s sample_auto_orient.jpg
ExifToolVersion                 : 9.63
FileName                        : sample_auto_orient.jpg
Directory                       : .
FileSize                        : 200 kB
FileModifyDate                  : 2014:06:12 11:29:05+09:00
FileAccessDate                  : 2014:06:12 11:29:06+09:00
FileInodeChangeDate             : 2014:06:12 11:29:05+09:00
FilePermissions                 : rw-r--r--
FileType                        : JPEG
MIMEType                        : image/jpeg
JFIFVersion                     : 1.01
ResolutionUnit                  : inches
XResolution                     : 1
YResolution                     : 1
ExifByteOrder                   : Big-endian (Motorola, MM)
Make                            : SonicGarden
Orientation                     : Horizontal (normal)
ColorSpace                      : sRGB
ExifImageWidth                  : 1024
ExifImageHeight                 : 768
ImageWidth                      : 1024
ImageHeight                     : 768
EncodingProcess                 : Baseline DCT, Huffman coding
BitsPerSample                   : 8
ColorComponents                 : 3
YCbCrSubSampling                : YCbCr4:2:0 (2 2)
ImageSize                       : 1024x768

OrientationがHorizontalになっています。つまりExif情報が適切に書き換えられた上で、画像の補正が行われているのですねー。なるほどなるほど。一安心です。

27
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
21