python 枕头 : Make image progressive before sending to 3rd party server

标签 python django python-imaging-library

我有一张图片,我正在使用 Django Forms 上传,它在变量中可用 InMemoryFile 我想要做的是让它渐进。

使图像渐进的代码

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

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

问题是,我必须保存文件以防我想让它渐进,然后再次打开它以将其发送到服务器。 (让它进步似乎是多余的 Action )

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

想法

寻找类似的东西。

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

最佳答案

如果您不想将中间文件保存到磁盘,您可以将它保存到 StringIOPIL.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 但希望如此。

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

关于 python 枕头 : Make image progressive before sending to 3rd party server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759712/

相关文章:

django - 如何重写 get_swagger_view 以使 django-rest-swagger 显示所有端点?

python - 使用 Python,有没有办法自动检测图像中的像素框?

python - 如何在 Python 中将 dos 路径拆分为其组件

python - Python 中 tkinter 和 tkinter.ttk 的小部件有什么区别?

python - Django 1.9 弃用警告 app_label

django - 摘要、基本和承载身份验证

python - 使用 Python(没有 SciPy)检测照片中的特定水印

Django - 无法在模型保存()中打开图像

python - 将 Spark Dataframe 中的 float 列转换为 VectorUDT

python - 为什么 VSCode 不从终端更改虚拟环境?