Python枕头:在发送到第三方服务器之前使图像渐进 [英] Python Pillow: Make image progressive before sending to 3rd party server

查看:108
本文介绍了Python枕头:在发送到第三方服务器之前使图像渐进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,我正在使用Django Forms上传,并且它在变量中可用 InMemoryFile 我想做的是使其渐进。



使图像渐进的代码



  img = Image.open )
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)

问题是,我必须保存文件,以防万一要使其渐进,并再次打开它将其发送到服务器。 (似乎是一个冗余的动作,使它渐进)



我只是想知道是否有一个图像渐进的,不会将图像物理地保存在磁盘上,在内存中,我可以使用现有代码上传吗?



想法



寻找类似的东西。

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


解决方案

想要将中间文件保存到磁盘,可以将其保存到 StringIO PIL.open() PIL.save()接受类似的对象以及文件名。

  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)

上传者需要支持使用 StringIO ,但希望是这样。 p>

可能使用合适的协程程序直接从 save()中流式传输结果,但这更多的工作。


I have an image that I am uploading using Django Forms, and its available in the variable as InMemoryFile What I want to do is to make it progressive.

Code to make an image a 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 issue is, I have to save the file in case I want to make it progressive, and open it again to send it to the server. (It seems a redundant actions to make it progressive)

I just want to know if there is anyway to make an image progressive which does not save the image physically on disk but in memory, which I can use the existing code to upload 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)

解决方案

If all you want is not saving the intermediate file to disk, you can save it to a StringIO. Both PIL.open() and PIL.save() accept file-like objects as well as filenames.

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 uploader needs to support working with the StringIO but that is hopefully the case.

It's probably possible to directly stream the result from save() using suitable coroutines, but that is a little more work.

这篇关于Python枕头:在发送到第三方服务器之前使图像渐进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆