美文网首页
convert_pdf2png【python把pdf转为png】

convert_pdf2png【python把pdf转为png】

作者: MYS_bio_man | 来源:发表于2022-12-08 21:29 被阅读0次

    话不多说,一看就懂!

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    from optparse import OptionParser
    import sys
    import re
    import os
    import datetime
    import fitz  
    
    
    def ck_in(file):
        if file.endswith(".pdf") or file.endswith(".PDF"):
            print("pdf file is %s" %file)
            return [file]
        else:
            if "/" in file:
                path = file
            else:
                path = os.getcwd()
            files = os.listdir(path)
            pdf_files = []
            for f in files:
                if f.endswith(".pdf") or f.endswith(".PDF"):
                    pdf_files.append(f)
            print("pdf files are:\n%s"%("\n".join(pdf_files)))
            return pdf_files
    
    
    def convert_pdf2png(pdfPath, imagePath): # in and out path
        a = ck_in(pdfPath)
    
    
    
        if imagePath.split()!=[]:
            path = imagePath
        else:
            path = os.getcwd()
        print(path)
        print("imagePath [or out path]=" + path)
        if not os.path.exists(path): 
            print("outPath not exists, creating now!")
            os.makedirs(path)
    
        for each in a:
            pdfDoc = fitz.open(each)
            i = 0
            for pg in range(pdfDoc.page_count): # each page
                i+=1
                print("handing the %s-st/nd/th page"%i) # i-st/nd/th page 
                page = pdfDoc[pg]
                rotate = int(0) # angle
                zoom_x = 2  # x size
                zoom_y = 2 # ysize
                mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
                pix = page.get_pixmap(matrix=mat, alpha=False)
                
                os.chdir(path)
                pix._writeIMG(path+"/"+each+"-%s-.png"%i,format=2)
    
    
    # convert_pdf2png("/Users/Downloads/pdf2png/","    ")
    
    
    
    # help, usage or args' definitions
    # aim of this script:
    # extract all PCG transcripts anotation in gtf
    parser = OptionParser()
    
    parser.add_option("-i", "--input", action="store", dest="input",
                      help="[input must set,default: cwd]", metavar='FILE', default=os.getcwd())
    parser.add_option("-o", '--output', action="store", dest="output",
                      help="output[default: cwd]", 
                      metavar='FILE', default=os.getcwd())
    (options, args) = parser.parse_args()
    
    input = str(options.input)
    output = str(options.output)
    convert_pdf2png(input,output)
    

    相关文章

      网友评论

          本文标题:convert_pdf2png【python把pdf转为png】

          本文链接:https://www.haomeiwen.com/subject/ddwafdtx.html