Skip to main content

Automatic memory management for Pillow - take the blue pill

Project description

BluePIL 💊

Take the blue pill. Your memory stays safe.

Stop fighting Pillow's memory leaks. BluePIL is a drop-in replacement that automatically handles cleanup so you don't have to.

The Problem

from PIL import Image

# Process 1000 images
for i in range(1000):
    img = Image.open(f'image_{i}.jpg')
    img = img.resize((800, 600))
    img.save(f'output_{i}.jpg')
    # img.close()  # Forgot this? RAM goes brrr 📈

Memory usage: 2GB+ and climbing 🔥

Here's the dirty secret: Pillow's Python objects get garbage collected just fine, but the underlying C image buffers don't get freed unless you explicitly call .close(). Python's GC has no idea those massive C allocations even exist.

The result? Your Python process looks lean to the GC while your actual RAM usage spirals out of control. You're forced to either:

  • Manually call .close() everywhere like it's 1995
  • Wrap everything in context managers
  • Just accept the leak and provision 40GB of RAM (the HuggingFace strategy)

The Solution

from BluePIL import Image

# Same code, zero leaks
for i in range(1000):
    img = Image.open(f'image_{i}.jpg')
    img = img.resize((800, 600))
    img.save(f'output_{i}.jpg')
    # Automatic cleanup ✨

Memory usage: Stable at 50MB

Installation

pip install BluePIL

Usage

Replace your Pillow import:

# Before
from PIL import Image

# After
from BluePIL import Image

That's it. Everything else works exactly the same.

How It Works

BluePIL wraps Pillow's Image class with automatic cleanup using weakref.finalize(). When an image goes out of scope, .close() is called automatically, releasing the C buffers immediately instead of leaking them forever.

Features

  • ✅ Drop-in replacement for PIL.Image
  • ✅ Zero API changes
  • ✅ Automatic memory cleanup
  • ✅ Context manager support
  • ✅ Works with existing Pillow code

Why "BluePIL"?

"You take the blue pill, the story ends. You wake up in your bed and believe whatever you want to believe."

Take the blue pill. Stop worrying about memory leaks. Just process your images.

License

MIT

Contributing

Issues and PRs welcome at github.com/ewan-m/BluePIL

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bluepil-0.1.5.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bluepil-0.1.5-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file bluepil-0.1.5.tar.gz.

File metadata

  • Download URL: bluepil-0.1.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for bluepil-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2eda483d4747ed69a71157803ba44125c0ad4f553d939c3f19f6028ef9cd2df1
MD5 87afe43e971951598f9c91b9940e7151
BLAKE2b-256 a37ece9b38a0b8596f62bd15767cc557e4318742e7d6201724f5f3e3dab8141f

See more details on using hashes here.

File details

Details for the file bluepil-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: bluepil-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for bluepil-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ca533b4a7d26c5e76283dcfa573d56f9c6eb4d91d848e40a4266b3e18a04cfdc
MD5 bfdc69293f24cd14ad4ad2385e288999
BLAKE2b-256 5078e418c6b7da2a2bc1861dade20c8fb9fa1ac52b7ed015de8356b9f8d6eaae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page