Practicing picture recognition is useful pil The module reads the picture , Save the code yourself .
Use the original size of the grayscale image when practicing 124k, Use PIL Save only 50k, Use opencv There is 127k.
Change the color picture to have the original picture 150k, Use PIL Save only 140k, Use opencv There is 270k.
import cv2
import numpy as np
from PIL import Image
# Use opencv Open the picture
img_cv = cv2.imread(‘./image/ceshi.jpeg’)
#opencv transformation PIL
img_pil = Image.fromarray(cv2.cvtColor(img_cv,cv2.COLOR_BGR2RGB))
# preservation
img_pil.save(‘./image/ceshi_img.jpeg’)
# View type
print(type(img_pil)) #<class ‘PIL.Image.Image’>
# Use PIL Open the picture
img_pil = Image.open(‘./image/ceshi.jpeg’)
#PIL transformation opencv
img_cv = cv2.cvtColor(np.asarray(img_pil),cv2.COLOR_RGB2BGR)
# preservation
cv2.imwrite(‘./image/ceshi_cv.jpeg’,img_cv)
# View type
print(type(img_cv)) #<class ‘numpy.ndarray’>