Fast loading and display of images in python (Utilizing Libjpeg Turbo in Python?)
I am currently creating an image viewer in python using Tkinter and PIL, but I want the image loading to be quicker. Therefore I would like to know how I could install and utilize Libjpeg Turbo with Tkinter and PIL, or even if I should use a different GUI toolkit like pyQT or a different imaging library (I found something called OpenCV?).
So essentially what is the best and fastest way I can load, resize, and display a large image in python?
EDIT: I believe the main bottleneck is image resizing, it is visible, but before that it is the image loading itself. I can not tell for sure though because I don't know how to measure such a thing as I am essentially a python newb.
And what I am doing is basically a minimalistic image viewer. I开发者_StackOverflow社区f you want to see my code it is here: http://pastebin.com/fwf8b0cU
EDIT 2: I have run through cProfile a bunch and have fixed some code, and fixed some slowness. So now my question is essentially should I be using another GUI toolkit like pyQT or a different imaging library (I found something called OpenCV?).
If you want to narrow down the source of the bottleneck I would start with some calls to timeit on your various sections of code. If that doesn't help or seems like too much effort, have a look into Python Profilers
You might take a look on PyTurboJPEG which is a Python wrapper of libjpeg-turbo with insanely fast rescaling (1/2, 1/4, 1/8) while decoding large JPEG image.
from turbojpeg import TurboJPEG
# specifying library path explicitly
# jpeg = TurboJPEG(r'D:\turbojpeg.dll')
# jpeg = TurboJPEG('/usr/lib64/libturbojpeg.so')
# jpeg = TurboJPEG('/usr/local/lib/libturbojpeg.dylib')
# using default library installation
jpeg = TurboJPEG()
# direct rescaling 1/2 while decoding input.jpg to BGR array
in_file = open('input.jpg', 'rb')
bgr_array_half = jpeg.decode(in_file.read(), scaling_factor=(1, 2))
in_file.close()
Old question but the answers here do not cover using it with PIL
The thread below explains a lot more, but the short version is:
conda uninstall -y --force pillow pil jpeg libtiff libjpeg-turbo
pip uninstall -y pillow pil jpeg libtiff libjpeg-turbo
conda install -yc conda-forge libjpeg-turbo
CFLAGS="${CFLAGS} -mavx2" pip install --upgrade --no-cache-dir --force-reinstall --no-binary :all: --compile pillow-simd
conda install -y jpeg libtiff
From: https://docs.fast.ai/performance.html#faster-image-processing
It only seems to work on Linux though. On MacOS X the Pillow-SIMD build doesn't seem to link to the libjpeg-turbo library properly.
Anyway, once compiled locally, pillow-simd replaces PIL and you get the acceleration of libjpeg-turbo and simd
精彩评论