import time
from io import BytesIO

import numpy as np
import cv2
# from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import requests
from PIL import Image
import skimage.io as io
import matplotlib.pyplot as plt
import matplotlib.image as mpig
from requests.packages import urllib3

'''
方式：                        返回类型
OpenCV                        np.ndarray
PIL                           PIL.JpegImagePlugin.JpegImageFile
keras.preprocessing.image     PIL.JpegImagePlugin.JpegImageFile
Skimage.io                    np.ndarray
matplotlib.pyplot             np.ndarray
matplotlib.image              np.ndarray
'''

imagePath="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F121420113514%2F201214113514-6-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670138586&t=bcf258851b92c61ca3ee2de96dd3df45"


'''
方式一：使用OpenCV
'''
# img1=cv2.imread(imagePath)
# print("img1:",img1.shape)
# print("img1:",type(img1))
# print("-"*10)


'''
方式二：使用PIL
'''
# img2=Image.open(imagePath)
# print("img2:",img2)
# print("img2:",type(img2))
# #转换成np.ndarray格式
# img2=np.array(img2)
# print("img2:",img2.shape)
# print("img2:",type(img2))
# print("-"*10)
#
#
# '''
# 方式三：使用keras.preprocessing.image
# '''
# img3=load_img(imagePath)
# print("img3:",img3)
# print("img3:",type(img3))
# #转换成np.ndarray格式,使用np.array(),或者使用keras里的img_to_array()
# #使用np.array()
# #img3=np.array(img2)
# #使用keras里的img_to_array()
# img3=img_to_array(img3)
# print("img3:",img3.shape)
# print("img3:",type(img3))
# print("-"*10)
#
#
'''
方式四：使用Skimage.io
'''
start = time.time()
# img4=io.imread(imagePath)
origin_file = urllib3.urlopen(imagePath).read()
# img = Image.open(origin_file)
print(time.time() - start)
#
#
# '''
# 方式五：使用matplotlib.pyplot
# '''
# img5=plt.imread(imagePath)
# print("img5:",img5.shape)
# print("img5:",type(img5))
# print("-"*10)
#
#
# '''
# 方式六：使用matplotlib.image
# '''
# img6=mpig.imread(imagePath)
# print("img6:",img6.shape)
# print("img6:",type(img6))
# print("-"*10)
start = time.time()
response = requests.get(imagePath)
image = Image.open(BytesIO(response.content))
img2=np.array(image)
print(time.time() - start, type(image))