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!
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