#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2022/5/6 11:06:36

@author: LiuKuan
@copyright: Apache License, Version 2.0
'''
import cv2
import numpy as np
#===============================================================================
'''  
# skimage 图像格式        通道：RGB 像素值：[0.0,1.0]      (h,w)
    
# Opencv  图像格式        通道：BGR 像素值：[0,255]        (h,w)
    gray是 (h,w)  
    color是(h,w,3)

# PIL     图像格式        (w,h)   
    
'''
#===============================================================================

__all__  = [
            'cv2pil', 'pil2cv',
            'cv2sk', 'sk2cv',
            'pil2sk', 'sk2pil',
            'iio2cv', 'cv2iio',
            ]


def cv2pil(img):
    from PIL import Image
    if img.ndim == 3 and img.shape[-1] == 3:
        img = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB), mode = 'RGB')
    else:
        img = Image.fromarray(img, mode = 'L')
    return img

def pil2cv(img):
    image = None
    if img.mode == 'RGB':
        image = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    elif img.mode == 'RGBA':
        image = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGBA2BGRA)
    elif img.mode == 'L':
        image = np.asarray(img)
    return image

def cv2sk(img):
    if img.ndim == 3:
        if img.shape[-1] == 3:
            img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        elif img.shape[-1] == 4:
            img = cv2.cvtColor(img,cv2.COLOR_BGRA2RGBA)
    return img

def sk2cv(img):
    if img.ndim == 3:
        if img.shape[-1] == 3:
            img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
        elif img.shape[-1] == 4:
            img = cv2.cvtColor(img,cv2.COLOR_RGBA2BGRA)
    return img

def pil2sk(img):
    pass

def sk2pil(img):
    pass

def iio2cv(img):
    if img.ndim == 3:
        image = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    elif img.ndim == 2:
        image = np.asarray(img)
    return image

def cv2iio(img):
    pass
