xxxxxxxxxx
def path_name(path_string):
set_path = path_string.replace("\\", "\\\\")
os.chdir(set_path)
def main():
p = input("Input the directory path: ")
path_name(p)
print(os.getcwd())
source = input("input the file name: ")
s = os.path.join(os.getcwd(), source)
print(s)
destination = input("input the new file name: ")
d = os.path.join(os.getcwd(), destination)
final = pdf2jpg.convert_pdf2jpg(s, d)
if __name__ == '__main__':
main()
xxxxxxxxxx
# Using ImageMagick:
convert -density 150 input.pdf -quality 90 output.jpg
xxxxxxxxxx
import sys
# Use command line arguments for input file
if len(sys.argv) < 2:
print("Usage: python extract_jpgs.py input.pdf")
sys.exit(1)
pdf_file_path = sys.argv[1]
startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
njpg = 0
with open(pdf_file_path, "rb") as pdf:
pdf_content = pdf.read()
i = 0
while True:
istream = pdf_content.find(b"stream", i)
if istream < 0:
break
istart = pdf_content.find(startmark, istream, istream+20)
if istart < 0:
i = istream+20
continue
iend = pdf_content.find(b"endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf_content.find(endmark, iend-20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
print("JPG {} from {} to {}".format(njpg, istart, iend))
jpg_content = pdf_content[istart:iend]
with open("jpg{}.jpg".format(njpg), "wb") as jpg_file:
jpg_file.write(jpg_content)
njpg += 1
i = iend
xxxxxxxxxx
# Extract jpg's from pdf's. Quick and dirty.
import sys
pdf = file(sys.argv[1], "rb").read()
startmark = "\xff\xd8"
startfix = 0
endmark = "\xff\xd9"
endfix = 2
i = 0
njpg = 0
while True:
istream = pdf.find("stream", i)
if istream < 0:
break
istart = pdf.find(startmark, istream, istream+20)
if istart < 0:
i = istream+20
continue
iend = pdf.find("endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend-20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
print "JPG %d from %d to %d" % (njpg, istart, iend)
jpg = pdf[istart:iend]
jpgfile = file("jpg%d.jpg" % njpg, "wb")
jpgfile.write(jpg)
jpgfile.close()
njpg += 1
i = iend