简体   繁体   English

Python Wand从PDF转换为JPG背景不正确

[英]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. 我在将pdf转换为jpeg时找到了一个如此有线的东西,所以我想弄清楚这可能是一个小bug。 See the converted jpg below, you could find that, the background color are all black. 看下面转换的jpg,你会发现,背景颜色都是黑色的。 The image is here: www.shdowin.com/public/02.jpg 图像在这里:www.shdowin.com/public/02.jpg

However, in the source file of pdf, you can see that the background color are normal white. 但是,在pdf的源文件中,您可以看到背景颜色是正常的白色。 The image is here: www.shdowin.com/public/normal.jpg 图像在这里: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. 我认为这可能是我的pdf文件的错,但是,当我尝试在.NET环境中使用Acrobat.pdf2image时,转换的jpg正确显示。

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 我已将pdf上传到url: 02.pdf

You can try... 你可以试试...

An easy solution is to change the order of commands: Change the format to jpeg first and then to resize 一个简单的解决方案是更改命令的顺序:首先将格式更改为jpeg,然后再调整大小

        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. 通过分辨率元组打开精确大小的PDF也很容易,因为分辨率可以是浮点数。

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: 对于仍然有这个问题的其他人,我通过谷歌搜索并尝试了几个小时,由于这个问题https://stackoverflow.com/a/40494320/2686243使用这两行:

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

Tried with Wand version 0.4.4 尝试使用Wand版本0.4.4

I got the answer by myself. 我自己得到了答案。 It's because of the alpha_channel case. 这是因为alpha_channel案例。 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. 这个pdf包括一些透明背景(在我转换为png格式之后),并且对于调整大小,ImageMagick选择最佳调整大小的滤镜,因此显示黑色背景。

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. 因此,经过大量实验,我发现只需在“with”语句中添加“img.alpha_channel = False”(在img.save()之前),这将正常工作。

Thanks for VadimR's advise, it is helpful. 感谢VadimR的建议,这很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM