如何加速枕头(python)中的图像加载?

Sal*_*tal 5 python image python-imaging-library

我想使用枕头进行一些简单的手写图像识别,它是实时的,所以我需要每秒调用我的函数 5-10 次。我正在加载图像并且只访问 20^2 像素中的 1 个,所以我真的不需要所有图像。我需要减少图像加载时间。

我从未使用过 python 图像库,并希望得到所有建议。

from PIL import Image
import time

start = time.time()

im = Image.open('ir/IMG-1949.JPG')
width, height = im.size
px = im.load()

print("loading: ", time.time() - start)
Run Code Online (Sandbox Code Playgroud)

期望加载时间:<50ms,实际加载时间:~150ms

Mar*_*ell 18

更新答案

自从我写下这个答案后,John Cupitt(作者pyvips)提出了一些改进和更正以及更公平的代码和时间安排,并在此处与大家分享。请查看他的改进版本,与下面的代码一起甚至优先查看我的代码。

原答案

JPEG 库具有“加载时收缩”功能,可以避免大量 I/O 和解压缩。您可以使用 PIL/PillowImage.draft()函数利用这一点,而不是像这样读取完整的 4032x3024 像素:

from PIL import Image

im = Image.open('image.jpg')
px = im.load() 
Run Code Online (Sandbox Code Playgroud)

在我的 Mac 上需要 297 毫秒,您可以执行以下操作并读取 1008x756 像素,即宽度的 1/4 和高度的 1/4:

im = Image.open('image.jpg') 
im.draft('RGB',(1008,756)) 
px = im.load()
Run Code Online (Sandbox Code Playgroud)

这只需 75 毫秒,即快 4 倍。


只是为了踢,我尝试比较各种技术如下:

#!/usr/bin/env python3 

import numpy as np 
import pyvips 
import cv2 
from PIL import Image 

def usingPIL(f): 
    im = Image.open(f) 
    return np.asarray(im) 

def usingOpenCV(f): 
    arr = cv2.imread(f,cv2.IMREAD_UNCHANGED) 
    return arr 

def usingVIPS(f): 
    image = pyvips.Image.new_from_file(f, access="sequential") 
    mem_img = image.write_to_memory() 
    imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)  
    return imgnp 

def usingPILandShrink(f): 
    im = Image.open(f)  
    im.draft('RGB',(1008,756))  
    return np.asarray(im) 

def usingVIPSandShrink(f): 
    image = pyvips.Image.new_from_file(f, access="sequential", shrink=4) 
    mem_img = image.write_to_memory() 
    imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)  
    return imgnp 
Run Code Online (Sandbox Code Playgroud)

并将其加载ipython并测试如下:

%timeit usingPIL('image.jpg')
315 ms ± 8.76 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit usingOpenCV('image.jpg')
102 ms ± 1.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit usingVIPS('image.jpg')
69.1 ms ± 31.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit usingPILandShrink('image.jpg')
77.2 ms ± 994 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit usingVIPSandShrink('image.jpg')                                                    
42.9 ms ± 332 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Run Code Online (Sandbox Code Playgroud)

看来pyVIPS显然是这里的赢家!

关键词:Python、PIL、枕头、图像、图像处理、JPEG、加载时收缩、加载时收缩、草稿模式、读取性能、加速。