| | import numpy as np |
| | import PIL.Image |
| | import PIL.ImageOps |
| |
|
| |
|
| | def exif_transpose(img): |
| | if not img: |
| | return img |
| |
|
| | exif_orientation_tag = 274 |
| |
|
| | |
| | if ( |
| | hasattr(img, "_getexif") |
| | and isinstance(img._getexif(), dict) |
| | and exif_orientation_tag in img._getexif() |
| | ): |
| | exif_data = img._getexif() |
| | orientation = exif_data[exif_orientation_tag] |
| |
|
| | |
| | if orientation == 1: |
| | |
| | pass |
| | elif orientation == 2: |
| | |
| | img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT) |
| | elif orientation == 3: |
| | |
| | img = img.rotate(180) |
| | elif orientation == 4: |
| | |
| | img = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT) |
| | elif orientation == 5: |
| | |
| | img = img.rotate(-90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT) |
| | elif orientation == 6: |
| | |
| | img = img.rotate(-90, expand=True) |
| | elif orientation == 7: |
| | |
| | img = img.rotate(90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT) |
| | elif orientation == 8: |
| | |
| | img = img.rotate(90, expand=True) |
| |
|
| | return img |
| |
|
| |
|
| | def load_image_file(file, mode="RGB"): |
| | |
| | img = PIL.Image.open(file) |
| |
|
| | if hasattr(PIL.ImageOps, "exif_transpose"): |
| | |
| | img = PIL.ImageOps.exif_transpose(img) |
| | else: |
| | |
| | img = exif_transpose(img) |
| |
|
| | img = img.convert(mode) |
| |
|
| | return np.array(img) |
| |
|