使用python比較PIL與OpenCV編碼圖片速度

以下代碼演示了使用python調用 pil和opencv分別往內存中編碼一個png或者jpeg圖片。通過結果可以簡單得出結論:

  1. 編碼png格式比jpeg格式耗時更多。
  2. 編碼jpeg格式pil與cv相差不大。
  3. 編碼png格式pil與cv相差較大。
import time

from io import StringIO, BytesIO
from PIL import Image

import numpy as np
import cv2


img_path = '/Users/zhangxin/pic/car.jpg'

# 1 使用PIL加載圖片
img = Image.open(img_path)
print("img size {}, mode {}".format(img.size, img.mode))
# 1.1 往內存中寫圖片——JPEG格式
t0 = time.time()
with BytesIO() as out:
    img.save(out, format='JPEG')
    t1 = time.time()
    with open('out_stream.jpg', 'wb') as fo:
        fo.write(out.getvalue())

# 1.2 往內存中寫圖片——PNG格式
t2 = time.time()
with BytesIO() as out:
    img.save(out, format='PNG')
    t3 = time.time()
    with open('out_stream.png', 'wb') as fo:
        fo.write(out.getvalue())

print("PIL save image as jpeg : ", t1 - t0)
print("PIL save image as png  : ", t3 - t2)

# 2 使用opencv 加載圖片
img = cv2.imread(img_path)
print("img shape : ", img.shape)

# 2.1 往內存中寫圖片——JPEG格式
t0 = time.time()
_, img_bytes = cv2.imencode(".jpg", img)
img_str = np.array(img_bytes).tostring()
t1 = time.time()
with open("cv_tmp.jpg", "wb") as f:
    f.write(img_str)

# 2.2 往內存中寫圖片——PNG格式
t2 = time.time()
_, img_bytes = cv2.imencode(".png", img)
img_str = np.array(img_bytes).tostring()
t3 = time.time()
with open("cv_tmp.png", "wb") as f:
    f.write(img_str)


print("OpenCV save image as jpeg : ", t1 - t0)
print("OpenCV save image as png  : ", t3 - t2)

輸出:

img size (800, 600), mode RGB
PIL save image as jpeg :  0.025282859802246094
PIL save image as png  :  0.2618370056152344
img shape :  (600, 800, 3)
OpenCV save image as jpeg :  0.02747821807861328
OpenCV save image as png  :  0.030751943588256836
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章