image processing - python set maximum file size when converting (pdf) to jpeg using e.g. Wand -
for subsequent processing purposes, in python converting multi-page pdf (f) jpegs (temp?.jpg):
import os wand.image import image wimage wimage(filename=f,resolution=300) img: 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 using wand because of ability sequence pdf pages, open pil etc.
i need resolution , compression_quality high possible, want each jpeg no larger (say) 300 kb in size.
how can set limit size of jpeg file?
on command line (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 managing -define attributes, unfortunately options locked wand.image.option frozenset. imho, renders whole feature unusable.
luckily, can create quick sub-class handle via wand.api.
import os wand.image import image wand.api import library wand.compat import binary class wimage(image): def mydefine(self, key, value): """ skip on wand.image.image.option """ return library.magicksetoption(self.wand, binary(key), binary(value)) wimage(filename=f, resolution=300) img: in range(len(img.sequence)): ftemp=os.path.abspath('temp%i.jpg'%i) wimage(img.sequence[i]) 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 near future. wand.image.option deprecated, , call img_to_save.options['jpeg:extent'] = '300kb'.
Comments
Post a Comment