0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PNG vs webP OpenCVでの読み書き対決

Posted at

PNG vs webP 試してみました.

OpenCVでwebPの画像が扱えることを知ったので,試してみました.

  • OpenCV: 4.10.0

Image Size: (1080, 1440, 3)の以下の写真(大空スバルちゃん多め)

syuba.jpg

試したこと:画像を10回連続保存して,保存した画像を10枚読み込む

##
# coding: utf-8
##

import os
import time
import numpy as np
import cv2

def main():
    print(f"OpenCV: {cv2.__version__}")

    img = cv2.imread("syuba.jpg")

    print(f"Image Size: {img.shape}")
    print("画像を10回連続保存して,保存した画像を10枚読み込む")
    print(f"Write Time")

    o_dir = "output/png"
    os.makedirs(o_dir, exist_ok=True)
    st = time.time()
    for i in range(10):
        cv2.imwrite(os.path.join(o_dir, f"test_{i}.png"), img)
    print(f"  PNG Time: {time.time() - st:.3f} [s]")

    o_dir = "output/webp"
    os.makedirs(o_dir, exist_ok=True)
    st = time.time()
    for i in range(10):
        cv2.imwrite(os.path.join(o_dir, f"test_{i}.webP"), img)
    print(f"  webP Time: {time.time() - st:.3f} [s]")

    print(f"Read Time")

    o_dir = "output/png"
    os.makedirs(o_dir, exist_ok=True)
    st = time.time()
    for i in range(10):
        pimg = cv2.imread(os.path.join(o_dir, f"test_{i}.png"))
    print(f"  PNG Time: {time.time() - st:.3f} [s]")

    o_dir = "output/webp"
    os.makedirs(o_dir, exist_ok=True)
    st = time.time()
    for i in range(10):
        pimg = cv2.imread(os.path.join(o_dir, f"test_{i}.webP"))
    print(f"  webP Time: {time.time() - st:.3f} [s]")

if __name__=="__main__":
    main()
実行結果
Write Time
  PNG Time: 0.431 [s]
  webP Time: 4.873 [s]
Read Time
  PNG Time: 0.265 [s]
  webP Time: 0.238 [s]

保存はめっちゃ遅いが,表示はPNGよりも若干早いです.
10枚ずつ保存した画像ディレクトリの容量は以下のようになりました.

  • output/png: 24.5 MB
  • output/webp: 14.7 MB

結果

画像をwebPで保存しておいて,プログラムから読み込むことしかしなければ,webPのほうが早くて軽そう.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?