python set maximum file size when converting (pdf) to jpeg using e.g. Wand

一笑奈何 提交于 2019-12-01 09:43:38

The library has wand.image.OptionDict for managing -define attributes, but unfortunately all options are locked by wand.image.Option frozenset. IMHO, this renders the whole feature as unusable.

Luckily, you can create a quick sub-class to handle this via the wand.api.

import os
from wand.image import Image
from wand.api import library
from wand.compat import binary

class wimage(Image):
    def myDefine(self, key, value):
        """ Skip over wand.image.Image.option """
        return library.MagickSetOption(self.wand, binary(key), binary(value))


with wimage(filename=f, resolution=300) as img:
    for i in range(len(img.sequence)):
        ftemp=os.path.abspath('temp%i.jpg'%i)
        with wimage(img.sequence[i]) as img_to_save:
            img_to_save.myDefine('jpeg:extent', '300kb')
            img_to_save.compression_quality = 100
            img_to_save.format='jpeg'
            img_to_save.save(filename=ftemp)

In the near future. The wand.image.Option would be deprecated, and you could simply call img_to_save.options['jpeg:extent'] = '300kb'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!