Python Wand converts from PDF to JPG background is incorrect

风流意气都作罢 提交于 2019-11-27 06:18:46

问题


I found a so wired thing while converting a pdf to jpeg, so i'd like to figure out that maybe this is a small bug. See the converted jpg below, you could find that, the background color are all black. The image is here: www.shdowin.com/public/02.jpg

However, in the source file of pdf, you can see that the background color are normal white. The image is here: www.shdowin.com/public/normal.jpg

I thought this maybe my pdf file's fault, however, when i try to use Acrobat.pdf2image in .NET environment, the converted jpg shows correctly.

Here is my code:

from wand.image import Image
from wand.color import Color
import os, os.path, sys

def pdf2jpg(source_file, target_file, dest_width, dest_height):
    RESOLUTION    = 300
    ret = True
    try:
        with Image(filename=source_file, resolution=(RESOLUTION,RESOLUTION)) as img:
            img.background_color = Color('white')
            img_width = img.width
            ratio     = dest_width / img_width
            img.resize(dest_width, int(ratio * img.height))
            img.format = 'jpeg'
            img.save(filename = target_file)
    except Exception as e:
        ret = False

    return ret

if __name__ == "__main__":
    source_file = "./02.pdf"
    target_file = "./02.jpg"

    ret = pdf2jpg(source_file, target_file, 1895, 1080)

Any suggestions for the issue?

I have uploaded the pdf to the url: 02.pdf

You can try...


回答1:


An easy solution is to change the order of commands: Change the format to jpeg first and then to resize

        img.format = 'jpeg'
        img.resize(dest_width, int(ratio * img.height))

It is also very easy to open the PDF in the exact size by the resolution tuple, because the resolution can be a float number.




回答2:


For others who still have this problem I fixed it after googling and trying a couple of hours thanks to this question https://stackoverflow.com/a/40494320/2686243 by using this two lines:

img.background_color = Color("white")
img.alpha_channel = 'remove'

Tried with Wand version 0.4.4




回答3:


I got the answer by myself. It's because of the alpha_channel case. This pdf includes some transparent background(after i transfomred to png format), and for resize, ImageMagick choose the best resize filter, so black background displayed.

So, after a lot of experiments, I found that just add "img.alpha_channel=False" in "with" statement(before img.save()), that would work properly.

Thanks for VadimR's advise, it is helpful.



来源:https://stackoverflow.com/questions/20439234/python-wand-converts-from-pdf-to-jpg-background-is-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!