python判断图片方向
时间: 2023-03-31 18:01:21 浏览: 177
可以使用Pillow库中的ExifTags模块来读取图片的Exif信息,从而判断图片的方向。具体实现可以参考以下代码:
```python
from PIL import Image, ExifTags
def get_image_orientation(image_path):
with Image.open(image_path) as img:
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
exif = dict(img._getexif().items())
if exif[orientation] == 3:
return 180
elif exif[orientation] == 6:
return 270
elif exif[orientation] == 8:
return 90
except (AttributeError, KeyError, IndexError):
pass
return
```
该函数会返回图片的方向角度,如果返回则表示图片方向正常。