Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

image - Python Wand converts from PDF to JPG background is incorrect

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...