简体   繁体   English

如何查找 pdf2image 生成的文件的文件名

[英]How to find the file name for files generated by pdf2image

I am trying to convert my pdf files to jpg .我正在尝试将我的pdf文件转换为jpg I first use pdf2image to save the file as a .ppm .我首先使用 pdf2image 将文件另存为.ppm Then I want to use PIL to convert the .ppm to .jpg .然后我想使用 PIL 将.ppm转换为.jpg

How do I find the name of the file that pdf2image saved?如何找到 pdf2image 保存的文件的名称?

Here is my code:这是我的代码:

def to_jpg(just_ids):
    for just_id in just_ids:
        image = convert_from_path('/Users/davidtannenbaum/Desktop/scraped/{}.pdf'.format(just_id), output_folder='/Users/davidtannenbaum/Desktop/scraped/')
        file_name = ?
        im = Image.open("/Users/davidtannenbaum/Desktop/scraped/{}.ppm".format(file_name))
        im.save("/Users/davidtannenbaum/Desktop/scraped/{}.jpg".format(just_id))

You don't need to, the image variable should contain a list of Image objects.你不需要, image变量应该包含Image对象的列表。 You can simply do:你可以简单地做:

for i, im in enumerate(image):
    im.save("/Users/davidtannenbaum/Desktop/scraped/{}_{}.jpg".format(just_id, i)))

The convert_to_path() method has a few more parameters you can use. convert_to_path()方法还有一些您可以使用的参数。 You can set the paths_only parameter to True and the format attribute fmt to "jpeg" .您可以将paths_only参数设置为True并将格式属性fmt设置为"jpeg"

This will directly save your images to your output folder in JPG format instead of PPM and the image variable will contain the relative paths to each image instead of the image objects.这将直接将您的图像以 JPG 格式而不是 PPM 格式保存到输出文件夹,并且image变量将包含每个图像而不是图像对象的相对路径。

for just_id in just_ids:
        image = convert_from_path('/Users/davidtannenbaum/Desktop/scraped/{}.pdf'.format(just_id), output_folder='/Users/davidtannenbaum/Desktop/scraped/', fmt="jpeg", paths_only=True)
pdf_path = '/path/to/pdf_images/'
output_folder = '/path/for/output/images/'

for pdf in os.listdir(pdf_path):
    filename = pdf.split('.')[0] # prepare your filename 
    pdfs = convert_from_path(os.path.join(pdf_path,pdf),output_folder=output_folder, output_file=os.path.join(output_folder+ filename), fmt="jpeg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM