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

◇◆丶佛笑我妖孽 提交于 2019-12-01 09:19:34

问题


For subsequent processing purposes, in python I am converting a multi-page PDF (f) into JPEGs (temp?.jpg):

import os
from wand.image import Image as wimage

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

I am using wand because of its ability to sequence the PDF pages, but am open to PIL etc.

I need the resolution and compression_quality to be as high as possible, but I want each JPEG to be no larger than (say) 300 kb in size.

How can I set a limit to the size of the JPEG file?

On the command line I would just do (see https://stackoverflow.com/a/11920384/1021819):

convert original.jpeg -define jpeg:extent=300kb -scale 50% output.jpg

Thanks!


回答1:


The wand 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'.



来源:https://stackoverflow.com/questions/45322213/python-set-maximum-file-size-when-converting-pdf-to-jpeg-using-e-g-wand

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