For subsequent processing purposes, in python I am converting a multi-page PDF (f
) into JPEGs (temp?.jpg
):
为了后续的处理目的,在python中,我正在将一个多页的PDF (f)转换为jpeg (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.
我之所以使用wand,是因为它可以对PDF页面进行排序,但对PIL等软件开放。
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.
我需要尽可能高的分辨率和compression_quality,但是我希望每个JPEG的大小不大于(比方说)300kb。
How can I set a limit to the size of the JPEG file?
如何限制JPEG文件的大小?
On the command line I would just do (see https://*.com/a/11920384/1021819):
在命令行上,我只需这样做(参见https://*.com/a/11920384/1021819):
convert original.jpeg -define jpeg:extent=300kb -scale 50% output.jpg
Thanks!
谢谢!
1 个解决方案
#1
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.
魔棒库有wand.image。用于管理-define属性的选项,但不幸的是所有选项都被wand.image锁定。选择frozenset。IMHO,这使整个特性变得不可用。
Luckily, you can create a quick sub-class to handle this via the wand.api
.
幸运的是,您可以通过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'
.
在不久的将来。wand.image。选项将被废弃,您可以简单地调用img_to_save。选择[' jpeg:程度上']= 300 kb。
#1
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.
魔棒库有wand.image。用于管理-define属性的选项,但不幸的是所有选项都被wand.image锁定。选择frozenset。IMHO,这使整个特性变得不可用。
Luckily, you can create a quick sub-class to handle this via the wand.api
.
幸运的是,您可以通过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'
.
在不久的将来。wand.image。选项将被废弃,您可以简单地调用img_to_save。选择[' jpeg:程度上']= 300 kb。