我正在尝试使用 imagemagick 将一些 PDF 转换为高分辨率 jpeg 。我正在使用 python 3.62 - 64 位和 wand 0.4.4 开发 win 10、64。在命令行我有:
$ /e/ImageMagick-6.9.9-Q16-HDRI/convert.exe -density 400 myfile.pdf -scale 2000x1000 test3.jpg.
这对我来说效果很好。
在Python中:
from wand.image import Image
file_path = os.path.dirname(os.path.abspath(__file__))+os.sep+"myfile.pdf"
with Image(filename=file_path, resolution=400) as image:
image.save()
image_jpeg = image.convert('jpeg')
这给了我低分辨率的 JPEG。我如何将其翻译成我的魔杖代码来完成同样的事情?
edit:
我意识到问题在于输入 pdf 必须作为二进制字符串读入 Image 对象,因此基于http://docs.wand-py.org/en/0.4.4/guide/read.html#read-blob http://docs.wand-py.org/en/0.4.4/guide/read.html#read-blob我试过:
with open(file_path,'rb') as f:
image_binary = f.read()
f.close()
with Image(blob=image_binary,resolution=400) as img:
img.transform('2000x1000', '100%')
img.make_blob('jpeg')
img.save(filename='out.jpg')
这可以正常读取文件,但输出被分成 10 个文件。为什么?我需要将其转换为 1 个高分辨率 jpeg。
EDIT:
我需要将 jpeg 发送到 OCR api,所以我想知道是否可以将输出写入类似对象的文件。看着https://www.imagemagick.org/api/magick-image.php#MagickWriteImageFile https://www.imagemagick.org/api/magick-image.php#MagickWriteImageFile, 我试过 :
emptyFile = Image(width=1500, height=2000)
with Image(filename=file_path, resolution=400) as image:
library.MagickResetIterator(image.wand)
# Call C-API Append method.
resource_pointer = library.MagickAppendImages(image.wand,
True)
library.MagickWriteImagesFile(resource_pointer,emptyFile)
这给出:
File "E:/ENVS/r3/pdfminer.six/ocr_space.py", line 113, in <module>
test_file = ocr_stream(filename='test4.jpg')
File "E:/ENVS/r3/pdfminer.six/ocr_space.py", line 96, in ocr_stream
library.MagickWriteImagesFile(resource_pointer,emptyFile)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type
我怎样才能让它发挥作用?