Image Processing

Libraries for manipulating images.

Pillow handles everyday edits, so it's the default Python image processing library. Use scikit-image for scientific analysis, and pyvips when memory is tight.

How to choose:

  • Resizing, cropping, and converting images: Pillow
  • Scientific image analysis on NumPy arrays: scikit-image
  • Large images, or many of them, with little memory: pyvips
  • ImageMagick's features from Python: Wand
  • Removing photo backgrounds: rembg
  • Resizing and cropping images on demand over HTTP: thumbor
  • QR codes: qrcode
  • Barcodes: python-barcode

Listed in editorial order, grouped by use case. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags

Barcodes and QR Codes 2 projects

A pure Python QR Code generator.
Create barcodes in Python with no extra dependencies.

General 5 projects

Pillow is the friendly PIL fork.
A Python library for (scientific) image processing.
A tool to remove image backgrounds.
danielgatis/github.com/danielgatis/rembg /25,223 stars /
Python bindings for MagickWand, C API for ImageMagick.
emcconville/github.com/emcconville/wand /1,482 stars /
A binding for libvips, a fast image processing library with low memory needs.
libvips/github.com/libvips/pyvips /815 stars /

Image Serving 1 project

A smart imaging service. It enables on-demand crop, resizing and flipping of images.
thumbor/github.com/thumbor/thumbor /10,528 stars /

Image Processing guide

Pillow is ideal for batch processing, like making thumbnails and converting between file formats. Open each file in a with Image.open(path) as im: block. Opening is fast and independent of the file size, since Pillow reads the pixels only when it has to. To fit an image to a size, use ImageOps.contain(), cover(), fit(), or pad(). thumbnail() works too, but it changes the image in place.

scikit-image aims to be the reference library for scientific image analysis in Python, and puts science ahead of photo editing. Images are plain NumPy arrays, so standard NumPy operations work on them. To change an image's dtype, use img_as_float() or img_as_ubyte(), never astype, which doesn't rescale the values to the new dtype's range.

pyvips builds a pipeline of operations and runs it only when you write the result. It streams the image a section at a time, so it doesn't keep whole images in memory. To shrink an image, use pyvips.Image.thumbnail() instead of resizing: it loads and resizes in one step, which is faster and uses less memory. When you read an image from top to bottom, open it with access="sequential".

Wand is a ctypes-based ImageMagick binding, so install ImageMagick's MagickWand library first. Its objects are resources like open files: use them in a with block so they get closed. Wand's docs say to never use Wand directly in an HTTP service or on any public server. Hand the images to a background worker through a queue, and limit ImageMagick's resources and formats in its policy.xml.

rembg runs as a CLI, a Python library, an HTTP server, or a Docker container. In code, create a session once with new_session() and pass it to each remove() call, since remove otherwise starts a new session every call. The model weights carry their own licenses, separate from rembg's MIT license, so check the one you use before you ship it in a commercial product.

thumbor is an HTTP server: you set the size and crop in the image URL, and it detects faces and important features to crop around them. Set a SECURITY_KEY so every URL is signed and nobody can tamper with it, and build those URLs in Python with libthumbor. In production, turn off ALLOW_UNSAFE_URL and run more than one instance behind a load balancer.

qrcode makes a QR code in one call: qrcode.make("Some data"). For more control, use the QRCode class, and pass version=None with make(fit=True) to pick the size for you. For SVG output, the README recommends the path factory, SvgPathImage. If you style the code or embed an image, set error correction to high, since styled codes aren't guaranteed to work with all readers.

python-barcode writes SVG with no external dependencies. For PNG and other images, install the python-barcode[images] extra, which adds Pillow. Its docs recommend SVG unless your target can't use it, since vectors scale better. It calculates the checksum for you.

Treat every uploaded image as untrusted. Pillow guards against decompression bombs with a pixel limit, and its Image.open(formats=...) argument restricts the formats it tries. With pyvips, check the image dimensions before you process it, and block the loaders libvips hasn't tested for security. With Wand, check each file's magic bytes, never its extension or MIME type.

Know a project that belongs here?

Tell us what it does and why it stands out.