convert_pdf2png【python把pdf转为png】

2022-12-08  本文已影响0人  MYS_bio_man

话不多说,一看就懂!

#!/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)
上一篇下一篇

猜你喜欢

热点阅读