Python Pillow: make a progressive image before sending to a third party server

I have an image that I am loading using Django Forms and it is available in a variable as InMemoryFile

What I want to do is make it progressive.

Code to make an image progressive

img = Image.open(source)
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)

      

Forms.py

my_file = pic.pic_url.file
photo = uploader.upload_picture_to_album(title=title, file_obj=my_file)

      

The problem is, I have to save the file in case I want to make it progressive, and reopen it to send it to the server. (The redundant actions seem to make it progressive)

I just want to know if there is a way to make the image progressive that doesn't store the image physically on disk, but in memory that I can use existing code to load it?

Idea

Looking for something similar.

    my_file=pic.pic_url.file
    progressive_file = (my_file)
    photo = picasa_api.upload_picture_to_album(title=title, file_obj=progressive_file)

      

+3


source to share


1 answer


If you want to save the intermediate file to disk, you can save it to StringIO

. Both PIL.open()

and PIL.save()

accept file objects and file names.

img = Image.open(source)
progressive_img = StringIO()
img.save(progressive_img, "JPEG", quality=80, optimize=True, progressive=True)
photo = uploader.upload_picture_to_album(title=title, file_obj=progressive_img)

      



The user needs to keep working with StringIO

, but hopefully it will.

It might be possible to directly pass the result from save()

using the appropriate coroutines, but that's a bit more.

+1


source







All Articles