首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Wand将pdf转换为黑色图像

Python Wand将pdf转换为黑色图像
EN

Stack Overflow用户
提问于 2017-09-29 22:51:47
回答 2查看 2.7K关注 0票数 2

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

代码语言:javascript
复制
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 )。我认为它来自一种透明层,但我还没有找到删除它的方法,尽管我做了几件事,例如

代码语言:javascript
复制
image.alpha_channel = False # made the same with True
image.background_color = Color('White')

在保存文件之前。我使用的是ImagemagickV6.9,因为V7无法使用Wand。

EN

回答 2

Stack Overflow用户

发布于 2017-10-07 02:44:38

我也有同样的问题,并解决了它,在这里检查我的答案:https://stackoverflow.com/a/46612049/2686243

添加

代码语言:javascript
复制
image.background_color = Color("white")
image.alpha_channel = 'remove'

解决了问题。

票数 4
EN

Stack Overflow用户

发布于 2017-10-02 20:50:12

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

代码语言:javascript
复制
cmd = "convert -units PixelsPerInch -density 300 -background white -flatten " + filename + " converted_pdf.jpg"
        print(cmd)
        os.system(cmd)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46491452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档