import os

import PIL.Image
import PIL.ImageOps
import numpy as np

count = 0
def exif_transpose(img, file):
    global count
    if not img:
        return img
    count += 1
    exif_orientation_tag = 274

    # Check for EXIF data (only present on some files)
    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]
        # print('hasattr')
        # Handle EXIF Orientation
        if orientation == 1:
            # Normal image - nothing to do!
            print(file, '1', count, img.size)
            pass
        elif orientation == 2:
            # Mirrored left to right
            print('2')
            img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 3:
            print('3')
            # Rotated 180 degrees
            img = img.rotate(180)
        elif orientation == 4:
            print('4')
            # Mirrored top to bottom
            img = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 5:
            print('5')
            # Mirrored along top-left diagonal
            img = img.rotate(-90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 6:
            print(file,'6', count, img.size)
            # Rotated 90 degrees
            img = img.rotate(-90, expand=True)
        elif orientation == 7:
            print('7')
            # Mirrored along top-right diagonal
            img = img.rotate(90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 8:
            print('8')
            # Rotated 270 degrees
            img = img.rotate(90, expand=True)

    return img


def load_image_file(file, mode='RGB'):
    # Load the image with PIL
    img = PIL.Image.open(file)

    # if hasattr(PIL.ImageOps, 'exif_transpose'):
        # Very recent versions of PIL can do exit transpose internally
        # print('if')
        # img = PIL.ImageOps.exif_transpose(img)
    # else:
        # Otherwise, do the exif transpose ourselves
    # print('else')
    img = exif_transpose(img, file=file)

    img = img.convert(mode)

    return np.array(img)

folder = 'data/rafeeq/test1/images/'
for i in os.listdir(folder):
    load_image_file(folder+i)
import os

import PIL.Image
import PIL.ImageOps
import numpy as np

count = 0
def exif_transpose(img, file):
    global count
    if not img:
        return img
    count += 1
    exif_orientation_tag = 274

    # Check for EXIF data (only present on some files)
    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]
        # print('hasattr')
        # Handle EXIF Orientation
        if orientation == 1:
            # Normal image - nothing to do!
            print(file, '1', count, img.size)
            pass
        elif orientation == 2:
            # Mirrored left to right
            print('2')
            img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 3:
            print('3')
            # Rotated 180 degrees
            img = img.rotate(180)
        elif orientation == 4:
            print('4')
            # Mirrored top to bottom
            img = img.rotate(180).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 5:
            print('5')
            # Mirrored along top-left diagonal
            img = img.rotate(-90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 6:
            print(file,'6', count, img.size)
            # Rotated 90 degrees
            img = img.rotate(-90, expand=True)
        elif orientation == 7:
            print('7')
            # Mirrored along top-right diagonal
            img = img.rotate(90, expand=True).transpose(PIL.Image.FLIP_LEFT_RIGHT)
        elif orientation == 8:
            print('8')
            # Rotated 270 degrees
            img = img.rotate(90, expand=True)

    return img


def load_image_file(file, mode='RGB'):
    # Load the image with PIL
    img = PIL.Image.open(file)

    # if hasattr(PIL.ImageOps, 'exif_transpose'):
        # Very recent versions of PIL can do exit transpose internally
        # print('if')
        # img = PIL.ImageOps.exif_transpose(img)
    # else:
        # Otherwise, do the exif transpose ourselves
    # print('else')
    img = exif_transpose(img, file=file)

    img = img.convert(mode)

    return np.array(img)

folder = 'data/rafeeq/test1/images/'
for i in os.listdir(folder):
    load_image_file(folder+i)
# Check for EXIF data (only present on some files) # print('hasattr') # Handle EXIF Orientation # Normal image - nothing to do! # Mirrored left to right # Rotated 180 degrees # Mirrored top to bottom # Mirrored along top-left diagonal # Rotated 90 degrees # Mirrored along top-right diagonal # Rotated 270 degrees # Load the image with PIL # if hasattr(PIL.ImageOps, 'exif_transpose'): # Very recent versions of PIL can do exit transpose internally # print('if') # img = PIL.ImageOps.exif_transpose(img) # else: # Otherwise, do the exif transpose ourselves # print('else')