Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster than PyGame for large image display?

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).

like image 375
Parand Avatar asked Jun 13 '11 08:06

Parand


2 Answers

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/

like image 95
David Koski Avatar answered Nov 13 '22 08:11

David Koski


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.

like image 28
Lilo Huang Avatar answered Nov 13 '22 07:11

Lilo Huang