我正在尝试通过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而失败。
答案 0 :(得分:2)
我遇到了同样的问题并修好了,请在此处查看我的答案:https://stackoverflow.com/a/46612049/2686243
添加
image.background_color = Color("white")
image.alpha_channel = 'remove'
解决了这个问题。
答案 1 :(得分:0)
因为我没有通过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)