ConvertToJpg.py

S. McKay, 02/10/2015 04:49 pm

Download (1.1 kB)

 
1
#!/usr/bin/python
2
import sys, os
3
import subprocess, time
4
5
6
class ConvertToJpg():
7
        """This package converts every pdf file in the current directory to jpg"""
8
9
        def main(self):
10
                "This runs the code from the command line"
11
                if not os.path.exists('./jpg'):
12
                        os.mkdir('./jpg')
13
                files = [f for f in os.listdir('.') if os.path.isfile(f)]
14
                t0=time.time()
15
                for file in files:
16
                        filename, fileext = os.path.splitext(file)
17
                        if fileext == '.pdf':
18
                                checkstring = 'gs -q -dNODISPLAY -c \"(./'+ filename + '.pdf) (r) file runpdfbegin pdfpagecount = quit\"';
19
                                lastPage=subprocess.check_output(checkstring,shell=True);
20
                                lastPage=lastPage.rstrip('\n');
21
                                commandstring = 'gs -dNumRenderingThreads=4 -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage='+lastPage+' -sOutputFile=./jpg/'+filename+'-%d.jpg -dJPEGQ=100 -r300 -q '+filename+'.pdf -c quit'
22
                                #commandstring = 'convert -density 600 -limit memory 1 -limit map 1 ' + file + ' png/' + filename+'.png'
23
                                os.system(commandstring)
24
                t1=time.time()
25
                elapsed=t1-t0
26
                print("Elapsed time=%f" % elapsed)
27
28
if __name__ == "__main__":
29
        ES = ConvertToJpg()
30
        ES.main()