我有一个使用 Django Forms 上传的图像,它在变量中可用InMemoryFile
我想做的就是让它进步。
使图像成为渐进式的代码
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)
问题是,我必须保存文件,以防我想让它渐进,然后再次打开它以将其发送到服务器。 (让它进步似乎是多余的动作)
我只是想知道是否有办法使图像渐进,它不会将图像物理保存在磁盘上而是保存在内存中,我可以使用现有的代码上传它?
Idea
寻找类似的东西。
my_file=pic.pic_url.file
progressive_file = (my_file)
photo = picasa_api.upload_picture_to_album(title=title, file_obj=progressive_file)
如果您不想将中间文件保存到磁盘,则可以将其保存到StringIO
. Both PIL.open()
and PIL.save()
accept 类似文件的对象以及文件名。
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
但希望情况确实如此。
可能可以直接流式传输结果save()
使用合适的协程,但这需要更多的工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)