<reponame>plt-francesco/useful-stuff<filename>pdf2jpg.py<gh_stars>0
from pdf2image import convert_from_path as convert_pdf_to_jpg
from img2pdf import convert as convert_jpg_to_pdf
from sys import argv
DPI = 500


if __name__ == "__main__":
    
    if(argv[1][-4:] == '.pdf'):

        print('converting  ' + argv[1] + 'from pdf to jpg...')
        pages = convert_pdf_to_jpg(argv[1], DPI)
        
        i = 1
        for page in pages:
            export_fname = argv[1][:-4] + ' [page ' + str(i) + '].jpg'
            print('exporting ' + export_fname)
            page.save(export_fname, 'JPEG')
            i = i + 1
    
    if((argv[1][-4:] == '.jpg') or (argv[1][-4:] == '.jpeg')):

        print('converting  ' + argv[1] + 'from jpg to pdf...')
        with open(argv[1],"wb") as f:

            export_fname = argv[1][:-4] + '.jpg'
            print('exporting' + export_fname)
            f.write(convert_jpg_to_pdf(argv[1]))

from pdf2image import convert_from_path as convert_pdf_to_jpg
from img2pdf import convert as convert_jpg_to_pdf
from sys import argv
DPI = 500


if __name__ == "__main__":
    
    if(argv[1][-4:] == '.pdf'):

        print('converting  ' + argv[1] + 'from pdf to jpg...')
        pages = convert_pdf_to_jpg(argv[1], DPI)
        
        i = 1
        for page in pages:
            export_fname = argv[1][:-4] + ' [page ' + str(i) + '].jpg'
            print('exporting ' + export_fname)
            page.save(export_fname, 'JPEG')
            i = i + 1
    
    if((argv[1][-4:] == '.jpg') or (argv[1][-4:] == '.jpeg')):

        print('converting  ' + argv[1] + 'from jpg to pdf...')
        with open(argv[1],"wb") as f:

            export_fname = argv[1][:-4] + '.jpg'
            print('exporting' + export_fname)
            f.write(convert_jpg_to_pdf(argv[1]))