【python】圖像相關小記

打開圖片:

PIL:

PIL_img=Image.open(path)

openCV:

cv.imread(

圖片尺寸:

PIL:

PIL_img.size[1]
PIL_img.size[0]

openCV:

img.shape[0]
img.shape[1]

圖片轉bytes:

PIL:

imgByteArr = BytesIO()
PIL_img.save(imgByteArr, format='PNG')

openCV:



圖片bytes轉str unicode編碼:

PIL:

img_byte = base64.b64encode(imgByteArr.getvalue())
img_str = img_byte.decode('ascii')

openCV:



str unicode編碼 轉 圖片bytes:

PIL:

img_decode_ = img_str.encode('ascii') #從unicode變成ascii編碼
img_decode = base64.b64decode(img_decode_)

openCV:



圖片bytes 轉 圖片:

PIL:


openCV:

img_np_ = np.frombuffer(img_decode, np.uint8)
img = cv2.imdecode(img_np_, cv2.COLOR_RGB2BGR) #轉爲opencv格式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章