1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 17:56:30 +00:00

pdfjpgextract: Python3 update

This commit is contained in:
Paweł Jastrzębski
2014-01-16 12:48:04 +01:00
parent 6d445ae151
commit 450076e6e8
2 changed files with 7 additions and 12 deletions

View File

@@ -411,7 +411,7 @@ class WorkerThread(QtCore.QThread):
GUI.progress.content = '' GUI.progress.content = ''
self.errors = True self.errors = True
MW.addMessage.emit(str(warn), 'warning', False) MW.addMessage.emit(str(warn), 'warning', False)
MW.addMessage.emit('Failed to create output file!', 'warning', False) MW.addMessage.emit('Failed to create output file!', 'error', False)
MW.addTrayMessage.emit('Failed to create output file!', 'Critical') MW.addTrayMessage.emit('Failed to create output file!', 'Critical')
except Exception as err: except Exception as err:
GUI.progress.content = '' GUI.progress.content = ''

View File

@@ -28,7 +28,6 @@ from random import choice
from string import ascii_uppercase, digits from string import ascii_uppercase, digits
#TODO: Check entire code. Replacing file() with open() is not enought.
class PdfJpgExtract: class PdfJpgExtract:
def __init__(self, origFileName): def __init__(self, origFileName):
self.origFileName = origFileName self.origFileName = origFileName
@@ -40,38 +39,34 @@ class PdfJpgExtract:
return self.path return self.path
def extract(self): def extract(self):
pdf = open(self.origFileName, "r").read() pdf = open(self.origFileName, "rb").read()
startmark = b"\xff\xd8"
startmark = "\xff\xd8"
startfix = 0 startfix = 0
endmark = "\xff\xd9" endmark = b"\xff\xd9"
endfix = 2 endfix = 2
i = 0 i = 0
njpg = 0 njpg = 0
os.makedirs(self.path) os.makedirs(self.path)
while True: while True:
istream = pdf.find("stream", i) istream = pdf.find(b"stream", i)
if istream < 0: if istream < 0:
break break
istart = pdf.find(startmark, istream, istream + 20) istart = pdf.find(startmark, istream, istream + 20)
if istart < 0: if istart < 0:
i = istream + 20 i = istream + 20
continue continue
iend = pdf.find("endstream", istart) iend = pdf.find(b"endstream", istart)
if iend < 0: if iend < 0:
raise Exception("Didn't find end of stream!") raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend - 20) iend = pdf.find(endmark, iend - 20)
if iend < 0: if iend < 0:
raise Exception("Didn't find end of JPG!") raise Exception("Didn't find end of JPG!")
istart += startfix istart += startfix
iend += endfix iend += endfix
jpg = pdf[istart:iend] jpg = pdf[istart:iend]
jpgfile = open(self.path + "/jpg%d.jpg" % njpg, "wb") jpgfile = open(self.path + "/jpg%d.jpg" % njpg, "wb")
jpgfile.write(bytearray(jpg, 'utf-8')) jpgfile.write(jpg)
jpgfile.close() jpgfile.close()
njpg += 1 njpg += 1
i = iend i = iend
return self.path, njpg return self.path, njpg