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!