【发布时间】: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 来应用背景颜色