How to perform JPEG compression in Python without writing/reading

42.1k Views Asked by At

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?

3

There are 3 best solutions below

5
On BEST ANSWER

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.

0
On

Using BytesIO

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

def generate(self, image, format='jpeg'):
    im = self.generate_image(image)
    out = BytesIO()
    im.save(out, format=format,quality=75)
    out.seek(0)
    return out

StringIO is missing in Python3.0, ref to : StringIO in python3

0
On

As of January 2024, you can achieve what you want like this (updated version of @Sam Gammon's answer, tested using Python 3.9):

from io import BytesIO

from PIL import Image

img = Image.open("test.jpg")

buffer = BytesIO()
img.save(buffer, "JPEG", quality=10)

with open("./test_quality_10.jpg", "wb") as handle:
    handle.write(buffer.getbuffer())