Skip to content

danschu/pygpujpeg

Repository files navigation

GPUJPEG - Python

Fast Jpeg Encoding/Decoding on GPU based on gpujpeg and nvjpeg-python.

Require

  • cuda >= 10.2
  • numpy >= 1.7
  • python >= 3.6
  • gcc >= 7.5
  • make >= 4.1

System

  • Windows (not tested on Linux yet)

Installing

Run build_and_test.cmd:

  • it downloads and builds gpujpeg
  • creates virtual environment ad activates it
  • installs numpy and opencv-python
  • compiles pygpujpeg and
  • runs a simple benchmark.

Usage

Have look at test.py.

0. Init PyGpuJpeg

from gpujpeg import GpuJpeg 
gp = GpuJpeg()

1. Use PyGpuJpeg

Read Jpeg File to Numpy

with open("testimg.jpg", "rb") as f:
    data = f.read()
img = gp.imdecode(data)

or

img = gp.imread("testimg.jpg")

Write Numpy to Jpeg File

data = gp.imencode(img, 85) 
with open(f"testimg.jpg", 'wb') as f:    
    f.write(data)   

or

gp.imwrite(img, "testimg.jpg", 85) 

3. FAQ

Q: Why is image decoding sometimes slow?

A: Huffman decoding can not be parallelized in gpujpeg, if no restart markers are set when image was encoded Pgpujpeg (based on gpujpeg) uses restart markers, therefore all images written with this library can be decoded much faster. If you are using OpenCV for encoding and need fast decoding, then set the restart marker in OpenCV:

params = [
    cv2.IMWRITE_JPEG_QUALITY, quality
    ,cv2.IMWRITE_JPEG_RST_INTERVAL, 8
]

imgj = cv2.imencode(".jpg", img, params)[1]

About

python wrapper for gpujpeg

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published