Imagemagick Python Magickwand pdf到图像转换和调整图像大小

Imagemagick Python Magickwand pdf到图像转换和调整图像大小,imagemagick,python-2.6,magickwand,Imagemagick,Python 2.6,Magickwand,我需要创建pdf文件的缩略图,我正在使用Imagemagick来实现这一点 我试过用Pythonmagick和wand将pdf转换成图像。但是,当我尝试调整转换后的pdf大小时,生成的图像变为黑色 是否有任何选项可以使用python包装器设置-define pdf:use cropbox=true Python中是否有其他方法将pdf转换为缩略图 代码如下: import wand img = wand.image.Image(filename="d:\\test.pdf[0]"

我需要创建pdf文件的缩略图,我正在使用Imagemagick来实现这一点

我试过用Pythonmagick和wand将pdf转换成图像。但是,当我尝试调整转换后的pdf大小时,生成的图像变为黑色

是否有任何选项可以使用python包装器设置
-define pdf:use cropbox=true

Python中是否有其他方法将pdf转换为缩略图

代码如下:

    import wand
    img = wand.image.Image(filename="d:\\test.pdf[0]")
    img.resize(160,160)
    img.save(filename="d:\\test.jpg")

我找到了解决这个问题的办法。 首先将pdf转换为图像并保存图像。打开新保存的图像并调整其大小

import wand
img = wand.image.Image(filename="d:\\test.pdf[0]")
img.save(filename="d:\\temp.jpg")
img = wand.image.Image(filename="d:\\temp.jpg")
img.resize(160,160)
img.save(filename="d:\\resized_image.jpg")

我仍在等待更好的答案。

如果您不依赖JPG,则无需使用临时文件即可完成

以下是我的作品:

import wand
img=wand.image.Image(filename="/home/vagrant/tmp/wand/law.pdf[0]")
img.format='png'
img.resize(220,220)
img.save(filename="/home/vagrant/tmp/wand/law_2.png")

我今天也面临同样的问题。我在其他帖子中找到了另一个解决方案。 图像变为黑色的原因是PDF文件中的背景是透明的。由于JPG文件无法识别alpha通道(记录透明像素信息)。JPG文件将这些透明像素设置为默认黑色

# Use the following two lines to fix this error
# img_page.background_color = Color('white')
# img_page.alpha_channel = 'remove'

with Image(filename="file.pdf",resolution= 350) as img_pdf:
    num_pages = len(img_pdf.sequence)
    for page, img in enumerate(img_pdf.sequence):
        img_page = Image(image=img)
        img_page.background_color = Color('white')
        img_page.alpha_channel = 'remove'
        img_page.resize(900,1200)
        img_page.save(filename="file"+str(page)+".jpg")
        if page == 0:
            img_page.resize(500, 500)
            img_page.save(filename="thumbnail.jpg")

ref:

我建议中间保存到“d:\\temp.jpg”时使用无损格式,以免在最终图像中引入任何额外的压缩伪影。