在python代码中,如何有效地将pdf中的某个页面保存为jpeg文件?(使用案例:我有一个python flask web服务器,上传pdf-s,每个页面对应的jpeg-s是商店.)
这个解决方案很接近,但问题是它没有将整个页面转换为jpeg.
我有一个在x轴上有时间戳的图和在y轴上有一些信号数据.作为文档,我想将带时间戳的图片与图中的特定点相关联.是否可以在绘图中绘制一条线到图中一系列图片中的图片?
我正在尝试将PDF转换为PNG - 这一切都运行正常,但是,即使我相信我已将其禁用,输出图像仍然是透明的:
with Image(filename='sample.pdf', resolution=300) as img:
img.background_color = Color("white")
img.alpha_channel = False
img.save(filename='image.png')
Run Code Online (Sandbox Code Playgroud)
以上产生图像但透明,我也试过以下:
with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
img.alpha_channel = False
img.save(filename='image.png')
Run Code Online (Sandbox Code Playgroud)
产生此错误:
Traceback (most recent call last):
File "file_convert.py", line 20, in <module>
with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
File "/Users/Frank/.virtualenvs/wand/lib/python2.7/site-packages/wand/image.py", line 1943, in __init__
raise TypeError("blank image parameters can't be used with image "
TypeError: blank image parameters can't be used with image opening parameters
Run Code Online (Sandbox Code Playgroud) 所以我的程序能够打开 PNG,但不能打开 PDF,所以我这样做只是为了测试,但它仍然无法打开,甚至是一个简单的 PDF。我不知道为什么。
from PIL import Image
with Image.open(r"Adams, K\a.pdf") as file:
print file
Traceback (most recent call last):
File "C:\Users\Hayden\Desktop\Scans\test4.py", line 3, in <module>
with Image.open(r"Adams, K\a.pdf") as file:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2590, in open
% (filename if filename else fp))
IOError: cannot identify image file 'Adams, K\\a.pdf'
Run Code Online (Sandbox Code Playgroud)
按照建议尝试 PyPDF2 后(顺便感谢您提供的链接),我的代码出现此错误。导入 PyPDF2
pdf_file= open(r"Adams, K (6).pdf", "rb")
read_pdf= PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
print number_of_pages
Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
Run Code Online (Sandbox Code Playgroud)