【问题标题】:Python Pillow: Make image progressive before sending to 3rd party serverPython Pillow:在发送到第 3 方服务器之前使图像渐进式
【发布时间】:2015-08-01 08:06:14
【问题描述】:

我有一张使用 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)

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

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

想法

寻找类似的东西。

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

【问题讨论】:

    标签: python django python-imaging-library pillow


    【解决方案1】:

    如果您不希望将中间文件保存到磁盘,您可以将其保存到StringIOPIL.open()PIL.save() 都接受 file-like 对象以及文件名。

    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() 流式传输结果,但这需要更多工作。

    【讨论】:

    • 拯救了我的一天;谢谢+1
    • jpeg 格式是否只提供渐进式图像,为什么不提供 png 格式的图像?我有一个疑问,就像我看到所有博客都在谈论渐进式 jpeg 而不是任何 png 格式的图像。有人可以澄清一下吗?
    • @siluverukirankumar 这与原始问题没有什么关系,但尽管如此:使用 PNG,使用Interlacing 实现渐进式编码/解码
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2019-04-04
    • 2020-05-02
    • 2011-02-13
    相关资源
    最近更新 更多