I am trying to obtain all the files names in a folder, and then use the same name to save a jpg. When i run this script, everything works. I do not have the luxury to write the file names myself everytime so I am passing in all the file names in the folder pdfs. Here is the output for the following code.
import ghostscript, os
from os import listdir
from os.path import isfile, join
def get_files(path):
input_files = [f for f in listdir(path) if isfile(join(path, f))]
return input_files
def pdf2jpeg(pdf_input_path, jpeg_output_path):
args = ["pdf2jpeg", # actual value doesn't matter
"-dNOPAUSE",
"-sDEVICE=jpeg",
"-dJPEGQ=95",
"-r600x600",
"-sOutputFile=" + jpeg_output_path,
pdf_input_path]
ghostscript.Ghostscript(*args)
if __name__ == '__main__':
input_files = get_files("pdfs")
pdf2jpeg("pdfs/test1.pdf", "jpgs/test1.jpg")
for input_file in input_files:
input_file_name = str("pdfs/"+str(input_file))
output_file_name = str('jpgs/'+str(input_file).replace(" ", "_").replace("pdf", "jpg"))#split(".")[0]
print input_file_name
print output_file_name
As you can see in the main function pdf2jpeg("pdfs/test1.pdf", "jpgs/test1.jpg")
OUTPUT = Copyright (C) 2015 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
GPL Ghostscript 9.18: Some glyphs of the font BAAAAA+ComicSansMS requires a patented True Type interpreter.
##### 37232128 c_void_p(37232128)
works fine. Now i want to loop through the imported file list. as you can see i am looping and printing to get the following OUTPUT:
##### 34209280 c_void_p(34209280)
pdfs/test1_(5th_copy).pdf
/jpgs/test1_(5th_copy).jpg
pdfs/test1_(copy).pdf
/jpgs/test1_(copy).jpg
pdfs/test1_(4th_copy).pdf
/jpgs/test1_(4th_copy).jpg
pdfs/test1_(3rd_copy).pdf
/jpgs/test1_(3rd_copy).jpg
pdfs/test1_(another_copy).pdf
/jpgs/test1_(another_copy).jpg
pdfs/test1.pdf
/jpgs/test1.jpg
pdfs/test1_(6th_copy).pdf
/jpgs/test1_(6th_copy).jpg
When add the following line to my loop,
for input_file in input_files:
input_file_name = str("pdfs/"+str(input_file))
output_file_name = str('jpgs/'+str(input_file).replace(" ", "_").replace("pdf", "jpg"))#split(".")[0]
print input_file_name
print output_file_name
pdf2jpeg(input_file_name, output_file_name)
# ADDED THIS LINE: pdf2jpeg(input_file_name, output_file_name)
# INSTEAD OF pdf2jpeg("pdfs/test1.pdf", "jpgs/test1.jpg")
I get this OUTPUT:
pdfs/test1 (copy).pdf
jpgs/test1_(copy).jpg
Traceback (most recent call last):
File "gsPdf2Jpg.py", line 27, in <module>
pdf2jpeg(str(input_file_name), str(output_file_name))
File "gsPdf2Jpg.py", line 17, in pdf2jpeg
ghostscript.Ghostscript(*args)
File "/home/trackstarz/prohealth/phenv/local/lib/python2.7/site-packages/ghostscript/__init__.py", line 157, in Ghostscript
stderr=kw.get('stderr', None))
File "/home/trackstarz/prohealth/phenv/local/lib/python2.7/site-packages/ghostscript/__init__.py", line 72, in __init__
rc = gs.init_with_args(instance, args)
File "/home/trackstarz/prohealth/phenv/local/lib/python2.7/site-packages/ghostscript/_gsprint.py", line 177, in init_with_args
raise GhostscriptError(rc)
How can I pass the correct directory/filename to run the function? I am using Ubuntu 14.06 python 2.7 and the Ghostscript library