OP 对“更快”的解决方案感兴趣,我对最快的解决方案很好奇,我正在尝试用真实世界的基准来回答这个问题。
我在比较:
我在 202897 主要是 JPG 文件上运行以下代码。
"""
pip install opsdroid-get-image-size --user
pip install pymage_size
pip install imagesize
"""
import concurrent.futures
from pathlib import Path
import cv2
import numpy as np
import pandas as pd
from tqdm import tqdm
from PIL import Image
import get_image_size
import imagesize
import pymage_size
files = [str(p.resolve())
for p in Path("/data/").glob("**/*")
if p.suffix in {".jpg", ".jpeg", ".JPEG", ".JPG", ".png", ".PNG"}]
def get_shape_cv2(fname):
img = cv2.imread(fname)
return (img.shape[0], img.shape[1])
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(get_shape_cv2, files), total=len(files)))
def get_shape_pil(fname):
img=Image.open(fname)
return (img.size[0], img.size[1])
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(get_shape_pil, files), total=len(files)))
def get_shape_scardine_size(fname):
try:
width, height = get_image_size.get_image_size(fname)
except get_image_size.UnknownImageFormat:
width, height = -1, -1
return (width, height)
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(get_shape_scardine_size, files), total=len(files)))
def get_shape_shibukawa(fname):
width, height = imagesize.get(fname)
return (width, height)
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(get_shape_shibukawa, files), total=len(files)))
def get_shape_pymage_size(fname):
img_format = pymage_size.get_image_size(fname)
width, height = img_format.get_dimensions()
return (width, height)
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(get_shape_pymage_size, files), total=len(files)))
结果:
-
cv2.imread: 8m23s
-
PIL.open: 2m00s
-
opsdroid/image_size: 29 秒
-
shibukawa/imagesize_py: 29 秒
-
kobaltcore/pymage_size: 29 秒
所以 opsdroid、shibukawa 和 kobaltcore 以相同的速度运行。现在对我来说另一个有趣的点是更好地了解哪些库具有最好的格式支持。
[编辑]
所以我继续测试快速库是否提供不同的结果:
# test if the libs provide the same results
def show_size_differences(fname):
w1, h1 = get_shape_scardine_size(fname)
w2, h2 = get_shape_pymage_size(fname)
w3, h3 = get_shape_shibukawa(fname)
if w1 != w2 or w2 != w3 or h1 != h2 or h2 != h3:
print(f"scardine: {w1}x{h1}, pymage: {w2}x{h2}, shibukawa: {w3}x{h3}")
with concurrent.futures.ProcessPoolExecutor(8) as executor:
results = list(tqdm(executor.map(show_size_differences, files), total=len(files)))
他们没有。