https://github.com/twtrubiks/python-exif-orientation
Automatically determine whether the photo has been rotated, if it is rotated, the photo is turned right
https://github.com/twtrubiks/python-exif-orientation
exif jpeg jpg orientation pillow png python tutorial
Last synced: 2 days ago
JSON representation
Automatically determine whether the photo has been rotated, if it is rotated, the photo is turned right
- Host: GitHub
- URL: https://github.com/twtrubiks/python-exif-orientation
- Owner: twtrubiks
- Created: 2017-02-27T11:24:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-17T05:19:23.000Z (about 6 years ago)
- Last Synced: 2025-04-15T11:55:09.319Z (6 months ago)
- Topics: exif, jpeg, jpg, orientation, pillow, png, python, tutorial
- Language: Python
- Homepage:
- Size: 329 KB
- Stars: 4
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-EXIF-Orientation
自動判斷照片是否有被旋轉過,如有被旋轉,將照片轉正 📝
* [Youtube Demo](https://youtu.be/o1KjeLUSCB8)
## 特色
* 透過 JPG/JPEG 裡的 EXIF Orientation 屬性,判斷照片是否有被旋轉過。
* 透過 [Pillow](https://pillow.readthedocs.io/en/stable/) 將照片轉正。
## 安裝套件
確定電腦有安裝 [Python](https://www.python.org/) 之後
clone 我的簡單範例
```
git clone https://github.com/twtrubiks/python-EXIF-Orientation.git
```接著請在 cmd (命令提示字元) 輸入以下指令
```
pip install pillow
```之前也有寫過一些 [Pillow](https://pillow.readthedocs.io/en/stable/) 的範例,可參考我之前寫的 [pillow-examples](https://github.com/twtrubiks/pillow-examples)
## 說明
關於 JPG/JPEG 裡的 EXIF-Orientation 參數,可以參考這張圖

從上圖可以發現
8 代表順時針轉90度
3 代表旋轉180度
6 代表逆時針90度
程式碼思路,只要先找到他們的 EXIF-Orientation 參數 (編號),也就是往哪個方向旋轉幾度,
再用 [Pillow](https://pillow.readthedocs.io/en/stable/) 反方向旋轉回來即可。
## 使用方法
將要轉正的圖片放在 [input_img](https://github.com/twtrubiks/python-EXIF-Orientation/tree/master/input_img) 資料夾裡
執行
```
python app.py
```會將轉正的圖片輸出在 [output_img](https://github.com/twtrubiks/python-EXIF-Orientation/tree/master/output_img) 資料夾裡
## 其他說明
這個方法只適用在 JPG/JPEG 裡,並不適用在 PNG裡面。
如果有一張圖片是從 PNG 轉成 JPG/JPEG,裡面的 EXIF-Orientation 這個參數也會消失。
## 執行環境
* Python 3.12.10
## Reference
* [Pillow](https://pillow.readthedocs.io/en/stable/)
## License
MIT license