【问题标题】:Python Wand convert pdf to black imagePython Wand将pdf转换为黑色图像
【发布时间】:2017-09-29 14:51:47
【问题描述】:

我正在尝试通过 Python 中的 Wand 将一些 pdf 文件转换为 jpg:

from wand.image import Image as Img
from wand.color import Color

    def importPdf(self):
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File",
                                                            QtCore.QDir.currentPath())
        print(filename)
        if not filename:
            print('error')
            return
        with Img(filename=filename,format='jpeg', resolution=300) as image:
            image.compression_quality = 99
            image.save(filename='file.jpeg')
            self.open_picture()

我的问题是它的结果是黑屏。转换适用于 png,但我无法执行 OCR(通过 png 上的 tesseract)。 我认为它来自一种透明层,但我还没有找到删除它的方法,虽然我做了几件事,例如

image.alpha_channel = False # made the same with True
image.background_color = Color('White')

在保存文件之前。 我正在使用 Imagemagick V6.9,因为 V7 使用 Wand 失败。

【问题讨论】:

  • JPG 不支持透明度。因此,如果您在输入文件中有它,则在保存为 JPG 时它将被删除。如果您必须有 JPG 输出,那么您需要在保存为 JPG 之前将结果与某种颜色对齐。 background_color 是一个设置。它没有运营商来应用它。您必须添加背景颜色,然后使用等效的 flatten 来应用背景颜色

标签: python wand


【解决方案1】:

我遇到了同样的问题并解决了,在这里查看我的答案:https://stackoverflow.com/a/46612049/2686243

添加

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

解决了这个问题。

【讨论】:

    【解决方案2】:

    因为我没有通过wand api找到-flatten,所以我最终通过imagemagick的os.system + convert.exe做到了。 它完成了这项工作。

    cmd = "convert -units PixelsPerInch -density 300 -background white -flatten " + filename + " converted_pdf.jpg"
            print(cmd)
            os.system(cmd)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2016-09-14
      • 2015-03-05
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多