照片方向 -- Exif Orientation
上周遇到一个问题发现有些用户手机竖着拍的照片,传上来后发现照片人头逆时针转了90度没有旋转。
其中一点让人比较费解,手机竖着拍照片,但是照片里面的 Exif 信息 Orientation: Rotate 90 CW
确实是横着的。
另外发现,检测头像是否旋转的代码有问题,忘记修改默认的读取器 ->setMetadataReader(new ExifMetadataReader())
1,无法提取出来照片的 Exif 中的方向信息。
Exif 简介
Exif 是专门为数码相机的照片设定的文件格式,可以记录数码照片的属性信息和拍摄数据。
Exif可以附加于JPEG、TIFF、RIFF等文件之中,为其增加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本信息。
Exif Orientation
Orientation 取值可以有八个,去掉镜面对称的四个 其实重要的就四个值 1 3 6 8
下图中 第一行 红框表示相机握持姿势,F 表示被拍着物体。
第二行 数字照片内容的示意图
第三行 数字照片在看图工具中,经过看图工具自动调整后的显示示意图。所以有时候想故意拍一个歪倒的照片来欣赏,并不容易看到,有时候工具就自动给你调正了。
程序中如何旋转调正呢?
就是下图中把 3 6 8 旋转调整到 1的位置,分别是旋转 +(-)180 -90 90,很明显角度的正负是以极坐标系的角度来判断正负的。
有意思的工具
exiftool
这是一个 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
Orientation : Rotate 90 CW3
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 顺时针旋转
Resource
-
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 ↩