pyvips: Python binding for libvips

3.1.1 · active · verified Mon Apr 13

pyvips is a Python binding for the high-performance libvips image processing library. It is designed for speed and low memory usage, particularly when handling large images, by employing a streaming, parallel processing pipeline that avoids loading entire images into memory. The library is actively maintained and supports Python versions 3.7 and newer.

Warnings

Install

Imports

Quickstart

This example loads an image, resizes it to 50% of its original dimensions, converts it to grayscale, and then saves it as a PNG file. It demonstrates the basic `new_from_file`, chained operations, and `write_to_file` methods. A dummy 'input.jpg' is created if not found for immediate execution.

import pyvips
import os

# Assuming 'input.jpg' exists in the current directory
# For demonstration, let's create a dummy image if not present
if not os.path.exists('input.jpg'):
    # In a real scenario, you'd have an actual image file.
    # For this quickstart, we'll create a simple black image.
    dummy_image = pyvips.Image.black(100, 100, bands=3)
    dummy_image.write_to_file('input.jpg')

# Load an image
image = pyvips.Image.new_from_file('input.jpg')

# Perform an operation, e.g., resize to 50% and convert to grayscale
processed_image = image.resize(0.5).colourspace('b-w')

# Save the processed image
processed_image.write_to_file('output_grayscale.png')

print(f"Processed image saved to output_grayscale.png (width: {processed_image.width}, height: {processed_image.height})")

view raw JSON →