imagemagick魔杖将pdf页面保存为图像 [英] imagemagick wand save pdf pages as images
问题描述
我想使用imagemagick Wand包将pdf文件的所有页面转换为单个图像文件。我遇到以下问题(请参阅下面的评论突出问题)
I would like to use imagemagick Wand package to convert all pages of a pdf file into a single image file. I am having the following trouble though (see comments below which highlight problem)
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)
我的最终目标是能够指定将哪些页面转换为一个连续图像。这可以从命令行中获得一点
My end goal it to be able to specify which pages are to be converted to one continual image. This is possible from the command line with a bit of
convert -append input.pdf[0-4]
但我正在尝试使用python。
but I am trying to work with python.
I看看我们可以通过这样做得到切片:
I see we can get slices by doing this:
[x for x in w.sequence[0:1]] # get page 1 and 2
现在它是如何将这些页面连接在一起的问题。
now its a question of how to join these pages together.
推荐答案
通过计算序列的长度,@ rikAtee回复/添加自动检测页数的略微简化:
A slight simplification of @rikAtee's answer / addition of detecting the page count automatically by counting the length of the sequence:
def convert_pdf_to_png(blob):
pdf = Image(blob=blob)
pages = len(pdf.sequence)
image = Image(
width=pdf.width,
height=pdf.height * pages
)
for i in xrange(pages):
image.composite(
pdf.sequence[i],
top=pdf.height * i,
left=0
)
return image.make_blob('png')
我没有注意到任何内存链接问题,虽然我的PDF只有2或3页。
I haven't noticed any memory link issues, although my PDFs only tend to be 2 or 3 pages.
这篇关于imagemagick魔杖将pdf页面保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!