Created
March 5, 2018 20:25
-
-
Save PureMath86/615fd132b7c01072dab8741cdeef79b6 to your computer and use it in GitHub Desktop.
pdf2img.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
with open(sys.argv[1],"rb") as file: | |
file.seek(0) | |
pdf = file.read() | |
startmark = b"\xff\xd8" | |
startfix = 0 | |
endmark = b"\xff\xd9" | |
endfix = 2 | |
i = 0 | |
njpg = 0 | |
while True: | |
istream = pdf.find(b"stream", i) | |
if istream < 0: | |
break | |
istart = pdf.find(startmark, istream, istream + 20) | |
if istart < 0: | |
i = istream + 20 | |
continue | |
iend = pdf.find(b"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] | |
with open("jpg%d.jpg" % njpg, "wb") as jpgfile: | |
jpgfile.write(jpg) | |
njpg += 1 | |
i = iend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment