{"library":"pyturbojpeg","title":"PyTurboJPEG: High-performance JPEG Handling","description":"PyTurboJPEG is a Python wrapper for the high-performance libjpeg-turbo library, enabling fast decoding and encoding of JPEG images. It leverages the underlying C library for significant speed improvements over pure Python or slower image processing libraries. The library is actively maintained with regular releases, often reflecting updates and features from the upstream libjpeg-turbo project. Current version is 2.2.0.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install pyturbojpeg"],"cli":null},"imports":["from pyturbojpeg import TurboJPEG"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from pyturbojpeg import TurboJPEG, TJPF_BGR, TJ_BGR\nimport numpy as np\nimport os\n\n# Instantiate TurboJPEG. Optionally, pass the path to libturbojpeg.so.0, .dylib, or .dll\n# Example: jpeg = TurboJPEG('/usr/local/lib/libturbojpeg.so.0')\njpeg = TurboJPEG()\n\n# --- Encode Example ---\n# Create a dummy BGR image (e.g., from OpenCV or Pillow in BGR mode)\nwidth, height = 640, 480\n# For simplicity, create a blank image; in real use, this would be actual image data\n# PyTurboJPEG expects contiguous C-style arrays for input\ndummy_img_array = np.zeros((height, width, 3), dtype=np.uint8)\n# Set a pixel to demonstrate it's not entirely blank, just to ensure data is there\ndummy_img_array[50, 50] = [255, 0, 0] # Blue pixel\n\n# Encode the NumPy array (its byte representation) to JPEG\n# TJPF_BGR indicates the input pixel format\ntry:\n    jpeg_data = jpeg.encode(dummy_img_array.tobytes(), width, height, TJPF_BGR, quality=85)\n    print(f\"Successfully encoded JPEG data. Size: {len(jpeg_data)} bytes\")\n\n    # Save to a file for verification (optional)\n    # with open('output.jpg', 'wb') as f:\n    #     f.write(jpeg_data)\n\n    # --- Decode Example ---\n    # Decode the JPEG data back to raw pixel data\n    # TJ_BGR indicates the desired output colorspace (constant for BGR output)\n    decoded_image_data, decoded_width, decoded_height, decoded_pix_fmt, decoded_colorspace = \\\n        jpeg.decode(jpeg_data, TJ_BGR)\n\n    print(f\"Decoded image: {decoded_width}x{decoded_height}, \"\n          f\"Pixel Format: {decoded_pix_fmt}, Colorspace: {decoded_colorspace}\")\n\n    # Convert decoded raw data back to a NumPy array\n    decoded_array = np.frombuffer(decoded_image_data, dtype=np.uint8).reshape((decoded_height, decoded_width, 3))\n    print(f\"Decoded NumPy array shape: {decoded_array.shape}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure libjpeg-turbo is correctly installed and discoverable by PyTurboJPEG.\")\n    print(\"You might need to set LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (macOS), or PATH (Windows).\")\n    print(\"Alternatively, explicitly pass the library path to TurboJPEG() constructor.\")\n\n","lang":"python","description":"This quickstart demonstrates how to initialize PyTurboJPEG, create a dummy image (using NumPy), encode it into JPEG format, and then decode the JPEG data back into a raw pixel buffer, converting it back to a NumPy array for further processing. It includes error handling guidance for common issues related to the libjpeg-turbo dependency.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}