I'm using PyGame to display images (photos). For larger image sizes the load and convert process is slow (eg. taking 2-3 seconds for an image of size 6000x4485). The actual code that's slow is:
image = pg.image.load(fname).convert()
Is there an alternative library or method that'll give better performance? My target platforms are windows7 and os x, and I'm ok with separate solutions for each (although a single solution would be better).
If your jpeg library supports it you can set the scaling parameters. You probably don't need a 6kx4k output image if you are showing it on the screen and it can speed up by a factor of several.
http://jpegclub.org/djpeg/
You can give a try 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 like the following,
import pygame
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 RGBA pixel array
pixel_array = jpeg.decode(
open('input.jpg', 'rb').read(),
pixel_format=TJPF_RGBA,
scaling_factor=(1, 2))
height, width, _ = pixel_array.shape
image = pygame.image.frombuffer(pixel_array.data, (width, height), 'RGBA')
libjpeg-turbo prebuilt binaries for macOS and Windows 7 are also available here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With