导入库
from PIL import Image
import cv2
import numpy as np
PIL中的resize
img_pil = Image.open("1.jpg")
img_pil = img_pil.resize((512, 512))
print(type(img_pil)) # <class 'PIL.Image.Image'>
img_pil.show()
cv2.resize()
img_cv2 = cv2.imread("1.jpg")
img_cv2 = cv2.resize(img_cv2,(512, 512))
print(type(img_cv2)) # <class 'numpy.ndarray'>
cv2.imshow("img_cv2", img_cv2)
cv2.imwrite("image.jpg", img_cv2) # 保存resize之后的图片
cv2.waitKey()
图片和上面的一样
cv2到PIL的转换
img_from_cv2 = Image.fromarray(img_cv2)
img_from_cv2.show()
重点来了,显示的图像不一样,目前没有找到原因,有可能是numpy数组类型的原因,还没有测试
PIL到cv2的转换
img_from_pil = np.asarray(img_pil)
cv2.imwrite("img_from_pil.jpg", img_from_pil)
cv2.imshow("img_from_pil", img_from_pil)
cv2.waitKey()
可以看到从PIL转到cv2之后,图像也会发生变化