Search code examples
pythonimagepdfimagemagickpythonmagick

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


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.


Solution

  • 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.