Losing quality of image while changing from pdf to image using PythonMagick

1.8k Views Asked by At

I am working on changing pdfs to images using PythonMagick. I am successfully changing the formats, but the quality of the image is diminished during this process.

This is the code that i am using.

sample_pdf="test_pdf"
sample_image="test_image"

pdf='/home/path/'+sample_pdf+''
image='/home/path/images/'+sample_image+''

im = PythonMagick.Image(pdf)
im.write(image)

I am losing the quality of image by this process.

While researching i found that the below code helps in retaining the quality of the image by using ImageMagick

convert -density 300 source.pdf -quality 80 target.jpg

is there something similar in PythonMagick? I cant seem to find any, online.

Thanks in advance.

2

There are 2 best solutions below

1
On BEST ANSWER

Have you tried the density and quality methods of your instance?

sample_pdf="test_pdf"
sample_image="test_image"

pdf='/home/path/{}'.format(sample_pdf)
image='/home/path/images/{}'.format(sample_image)

im = PythonMagick.Image(pdf)
im.density("300")
im.quality(80)
im.write(image)

You should have look at the API documentation.

0
On
import PythonMagick

sample_pdf="test_pdf"
sample_image="test_image"

pdf='/home/path/'+sample_pdf+''
image='/home/path/images/'+sample_image+''

im = PythonMagick.Image()
im.density("300")
im.read(pdf)
im.quality(100)
im.write(image)

This worked like a charm for me. Thanks once again Payet.