Search code examples
pythonimagejpegpython-imaging-library

How to perform JPEG compression in Python without writing/reading


I'd like to work directly with compressed JPEG images. I know that with PIL/Pillow I can compress an image when I save it, and then read back the compressed image - e.g.

from PIL import Image
im1 = Image.open(IMAGE_FILE)
IMAGE_10 = os.path.join('./images/dog10.jpeg')
im1.save(IMAGE_10,"JPEG", quality=10)
im10 = Image.open(IMAGE_10)

but, I'd like a way to do this without the extraneous write and read. Is there some Python package with a function that will take an image and quality number as inputs and return a jpeg version of that image with the given quality?


Solution

  • For in-memory file-like stuff, you can use StringIO. Take a look:

    from io import StringIO # "import StringIO" directly in python2
    from PIL import Image
    im1 = Image.open(IMAGE_FILE)
    
    # here, we create an empty string buffer    
    buffer = StringIO.StringIO()
    im1.save(buffer, "JPEG", quality=10)
    
    # ... do something else ...
    
    # write the buffer to a file to make sure it worked
    with open("./photo-quality10.jpg", "w") as handle:
        handle.write(buffer.contents())
    

    If you check the photo-quality10.jpg file, it should be the same image, but with 10% quality as the JPEG compression setting.