上周遇到一個問題發現有些用戶手機豎著拍的照片,傳上來後發現照片人頭逆時針轉了90度沒有旋轉。
其中一點讓人比較費解,手機豎著拍照片,但是照片裡面的 Exif 信息 Orientation: Rotate 90 CW 確實是橫著的。
另外發現,檢測頭像是否旋轉的代碼有問題,忘記修改默認的讀取器 ->setMetadataReader(new ExifMetadataReader())1,無法提取出來照片的 Exif 中的方向信息。
Exif 簡介Exif 是專門為數位相機的照片設定的文件格式,可以記錄數碼照片的屬性信息和拍攝數據。
Exif可以附加於JPEG、TIFF、RIFF等文件之中,為其增加有關數位相機拍攝信息的內容和索引圖或圖像處理軟體的版本信息。
Exif OrientationOrientation 取值可以有八個,去掉鏡面對稱的四個 其實重要的就四個值 1 3 6 8
下圖中 第一行 紅框表示相機握持姿勢,F 表示被拍著物體。
第二行 數字照片內容的示意圖
第三行 數字照片在看圖工具中,經過看圖工具自動調整後的顯示示意圖。所以有時候想故意拍一個歪倒的照片來欣賞,並不容易看到,有時候工具就自動給你調正了。
程序中如何旋轉調正呢?就是下圖中把 3 6 8 旋轉調整到 1的位置,分別是旋轉 +(-)180 -90 90,很明顯角度的正負是以極坐標系的角度來判斷正負的。
有意思的工具這是一個 perl 寫的庫,可以跨平臺使用,並且提供了命令行應用程式,其他語言也有對其的封裝。這個工具功能強大,能提取各種文件裡面的元信息2。
安裝➜ yay exiftool
7 aur/ruby-mini_exiftool 2.10.0-2 (+0 0.00)
A wrapper for the ExifTool command-line application
6 aur/exiftool 12.09-1 (+1 0.40)
Meta information reader/writer
5 aur/jexiftoolgui 1.6.3-1 (+2 0.00)
jExifToolGUI is a java/Swing graphical frontend for the excellent command-line ExifTool application by Phil Harvey
4 aur/python2-pyexiftool 0.2.0-2 (+11 0.00) (孤立)
Library to communicate with an instance of ExifTool command-line application
3 aur/python-pyexiftool 0.2.0-2 (+11 0.00) (孤立)
Library to communicate with an instance of ExifTool command-line application
2 aur/pyexiftoolgui-git 0.5.r43.17c7a0e-1 (+17 0.00) (孤立)
A python pyside graphical frontend for the excellent open source command line tool ExifTool.
1 extra/perl-image-exiftool 12.00-2 (2.4 MiB 12.4 MiB) (已安裝)
Reader and rewriter of EXIF informations that supports raw files
==> 要安裝的包 (示例: 1 2 3, 1-3 或 ^4)
==> 1
[sudo] soul11201 的密碼:
警告:perl-image-exiftool-12.00-2 已經為最新 -- 重新安裝
正在解析依賴關係...
錯誤:local 資料庫不一致:名字和軟體包中的 electron-ssr-0.3.0 不一致
錯誤:local 資料庫不一致:版本和軟體包中的 electron-ssr-0.3.0 不一致
正在查找軟體包衝突...
軟體包 (1) perl-image-exiftool-12.00-2
全部安裝大小:12.39 MiB
淨更新大小:0.00 MiB
:: 進行安裝嗎?[Y/n]
(1/1) 正在檢查密鑰環裡的密鑰 [########################################] 100%
(1/1) 正在檢查軟體包完整性 [########################################] 100%
(1/1) 正在加載軟體包文件 [########################################] 100%
(1/1) 正在檢查文件衝突 [########################################] 100%
(1/1) 正在檢查可用存儲空間 [########################################] 100%
:: 正在處理軟體包的變化...
(1/1) 正在重新安裝 perl-image-exiftool [########################################] 100%
:: 正在運行事務後鉤子函數...
(1/2) Arming ConditionNeedsUpdate...
(2/2) Warn about old perl modules
➜
➜ exiftool 4bb1155c7d0bc9eb71ba7de55316786b_1604673582825.jpeg
ExifTool Version Number : 12.00
File Name : 4bb1155c7d0bc9eb71ba7de55316786b_1604673582825.jpeg
Directory : .
File Size : 1605 kB
File Modification Date/Time : 2020:11:06 23:22:37+08:00
File Access Date/Time : 2020:11:06 23:22:37+08:00
File Inode Change Date/Time : 2020:11:10 20:34:28+08:00
File Permissions : rw-r--r--
File Type : JPEG
File Type Extension : jpg
MIME Type : image/jpeg
JFIF Version : 1.01
Resolution Unit : None
X Resolution : 72
Y Resolution : 72
Exif Byte Order : Big-endian (Motorola, MM)
Orientation : Rotate 90 CW
Color Space : Uncalibrated
Exif Image Width : 3088
Exif Image Height : 2316
Current IPTC Digest : d41d8cd98f00b204e9800998ecf8427e
IPTC Digest : d41d8cd98f00b204e9800998ecf8427e
Profile CMM Type : Apple Computer Inc.
Profile Version : 4.0.0
Profile Class : Display Device Profile
Color Space Data : RGB
Profile Connection Space : XYZ
Profile Date Time : 2017:07:07 13:22:32
Profile File Signature : acsp
Primary Platform : Apple Computer Inc.
CMM Flags : Not Embedded, Independent
Device Manufacturer : Apple Computer Inc.
Device Model :
Device Attributes : Reflective, Glossy, Positive, Color
Rendering Intent : Perceptual
Connection Space Illuminant : 0.9642 1 0.82491
Profile Creator : Apple Computer Inc.
Profile ID : ca1a9582257f104d389913d5d1ea1582
Profile Description : Display P3
Profile Copyright : Copyright Apple Inc., 2017
Media White Point : 0.95045 1 1.08905
Red Matrix Column : 0.51512 0.2412 -0.00105
Green Matrix Column : 0.29198 0.69225 0.04189
Blue Matrix Column : 0.1571 0.06657 0.78407
Red Tone Reproduction Curve : (Binary data 32 bytes, use -b option to extract)
Chromatic Adaptation : 1.04788 0.02292 -0.0502 0.02959 0.99048 -0.01706 -0.00923 0.01508 0.75168
Blue Tone Reproduction Curve : (Binary data 32 bytes, use -b option to extract)
Green Tone Reproduction Curve : (Binary data 32 bytes, use -b option to extract)
Image Width : 3088
Image Height : 2316
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 : 3088x2316
Megapixels : 7.2
1 = Horizontal (normal)
2 = Mirror horizontal
3 = Rotate 180
4 = Mirror vertical
5 = Mirror horizontal and rotate 270 CW
6 = Rotate 90 CW
7 = Mirror horizontal and rotate 90 CW
8 = Rotate 270 CW
CW: clock wise 順時針旋轉
Resource45678
https://imagine.readthedocs.io/en/stable/usage/metadata.html#exif-metadata-reader ↩
https://exiftool.org/#supported ↩
https://exiftool.org/forum/index.php?topic=7909.0 ↩
https://zh.wikipedia.org/wiki/Exif ↩
https://www.impulseadventure.com/photo/exif-orientation.html ↩
https://blog.csdn.net/jfsdggsd/article/details/40050959 ↩
https://blog.csdn.net/wanshaobo888/article/details/94552288 ↩
https://blog.csdn.net/netdxy/article/details/49108489 ↩