我想使用imagemagick魔杖包將PDF文件的所有頁面轉換爲單個圖像文件。我雖然具有下列麻煩(見下面的註釋其中的突出問題)imagemagick魔杖保存pdf頁面作爲圖像
import tempfile
from wand.image import Image
with file('my_pdf_with_5_pages.png') as f:
image = Image(file=f, format='png')
save_using_filename(image)
save_using_file(image)
def save_using_filename(image):
with tempfile.NamedTemporaryFile() as temp:
# this saves all pages, but a file for each page (so 3 files)
image.save(filename=temp.name)
def save_using_file(image):
with tempfile.NamedTemporaryFile() as temp:
# this only saves the first page as an image
image.save(file=temp)
我的最終目標是要能夠指定哪些網頁將被轉換成一個連續的圖像。這是從命令行有點可能
convert -append input.pdf[0-4]
但我想與python一起工作。
我看我們可以通過這樣得到片:
[x for x in w.sequence[0:1]] # get page 1 and 2
現在它的如何將這些網頁連接起來的問題。
謝謝@Steve。運行代碼一個小時左右後,我注意到內存泄漏。你可以把你的代碼放在一個循環中,並在發生內存泄漏時回報。會很高興知道:) – rikAtee