diff --git a/pdfmajor/BMPWriter/__init__.py b/pdfmajor/BMPWriter/__init__.py new file mode 100644 index 0000000..2e93a9a --- /dev/null +++ b/pdfmajor/BMPWriter/__init__.py @@ -0,0 +1,60 @@ + +import struct +from io import BytesIO + +from ..parser.PDFStream import LITERALS_DCT_DECODE, PDFStream + +def align32(x): + return ((x+3)//4)*4 + +## BMPWriter +## +class BMPWriter(object): + + @classmethod + def write_all(cls, fp, stream: PDFStream, bits: int, width: float, height: float, step: float): + bmp = cls(fp, 1, width, height) + data = stream.get_data() + i = 0 + for y in range(height): + bmp.write_line(y, data[i:i+step]) + i += step + + def __init__(self, fp, bits, width, height): + self.fp = fp + self.bits = bits + self.width = width + self.height = height + if bits == 1: + ncols = 2 + elif bits == 8: + ncols = 256 + elif bits == 24: + ncols = 0 + else: + raise ValueError(bits) + self.linesize = align32((self.width*self.bits+7)//8) + self.datasize = self.linesize * self.height + headersize = 14+40+ncols*4 + info = struct.pack(' str: - if self.inactive: - return "N/A" - - stream = image.stream - filters = stream.get_filters() - (width, height) = image.srcsize - if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE: - ext = '.jpg' - elif (image.bits == 1 or - image.bits == 8 and image.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)): - ext = '.%dx%d.bmp' % (width, height) - else: - ext = '.%d.%dx%d.img' % (image.bits, width, height) - name = image.name+ext - path = os.path.join(self.outdir, name) - fp=open(path, 'wb') - if ext == '.jpg': - raw_data = stream.get_rawdata() - if LITERAL_DEVICE_CMYK in image.colorspace: - from PIL import Image - from PIL import ImageChops - ifp = BytesIO(raw_data) - i = Image.open(ifp) - i = ImageChops.invert(i) - i = i.convert('RGB') - i.save(fp, 'JPEG') - else: - fp.write(raw_data) - elif image.bits == 1: - bmp = BMPWriter(fp, 1, width, height) - data = stream.get_data() - i = 0 - width = (width+7)//8 - for y in range(height): - bmp.write_line(y, data[i:i+width]) - i += width - elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_RGB: - bmp = BMPWriter(fp, 24, width, height) - data = stream.get_data() - i = 0 - width = width*3 - for y in range(height): - bmp.write_line(y, data[i:i+width]) - i += width - elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_GRAY: - bmp = BMPWriter(fp, 8, width, height) - data = stream.get_data() - i = 0 - for y in range(height): - bmp.write_line(y, data[i:i+width]) - i += width - else: - fp.write(stream.get_data()) - fp.close() - return name diff --git a/pdfmajor/interpreter/commands/commands.py b/pdfmajor/interpreter/commands/commands.py index 59d9c3d..814e346 100644 --- a/pdfmajor/interpreter/commands/commands.py +++ b/pdfmajor/interpreter/commands/commands.py @@ -11,8 +11,6 @@ from .state import make_char_block, make_curve, make_image, make_xobject log = get_logger('commands') -class CommandError(Exception): pass - # No Support # @PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs') # def do_no_support(stack: PDFStateStack) -> PDFStateStack: @@ -20,15 +18,27 @@ class CommandError(Exception): pass # return stack # marked content operators -@PDFCommands.add('MP', 'DP') -def do_MP(stack: PDFStateStack) -> PDFStateStack: - log.warning("Unsupported command - do_tag") +@PDFCommands.add('MP') +def do_MP(stack: PDFStateStack, tag) -> PDFStateStack: + log.warning(f"Unsupported command - do_tag {[tag]}") # self.device.do_tag(tag) return stack -@PDFCommands.add('BMC', 'BDC') -def do_BMC(stack: PDFStateStack) -> PDFStateStack: - log.warning("Unsupported command - begin_tag") +@PDFCommands.add('DP') +def do_DP(stack: PDFStateStack, tag, props=None) -> PDFStateStack: + log.warning(f"Unsupported command - do_tag {[tag.name, type(tag), props]}") + # self.device.do_tag(tag) + return stack + +@PDFCommands.add('BMC') +def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack: + log.warning(f"Unsupported command - begin_tag {[tag.name, type(tag)]}") + # self.device.begin_tag(tag) + return stack + +@PDFCommands.add('BDC') +def do_BDC(stack: PDFStateStack, tag, props=None) -> PDFStateStack: + log.warning(f"Unsupported command - begin_tag {[tag.name, type(tag), props]}") # self.device.begin_tag(tag) return stack diff --git a/pdfmajor/interpreter/commands/state/layout/LTImage.py b/pdfmajor/interpreter/commands/state/layout/LTImage.py index 2eadaf3..f2e7bc1 100644 --- a/pdfmajor/interpreter/commands/state/layout/LTImage.py +++ b/pdfmajor/interpreter/commands/state/layout/LTImage.py @@ -1,7 +1,19 @@ +import os +from io import BytesIO + +import PIL as pillow + +from pdfmajor.BMPWriter import BMPWriter from pdfmajor.utils import Bbox -from pdfmajor.parser.PDFStream import PDFStream +from pdfmajor.parser.PDFStream import PDFStream, LITERALS_DCT_DECODE +from pdfmajor.parser.constants import LIT + from ._base import LTComponent +LITERAL_DEVICE_GRAY = LIT('DeviceGray') +LITERAL_DEVICE_RGB = LIT('DeviceRGB') +LITERAL_DEVICE_CMYK = LIT('DeviceCMYK') + ## LTImage ## class LTImage(LTComponent): @@ -22,4 +34,46 @@ class LTImage(LTComponent): def __repr__(self): return ('<%s(%s) %s %r>' % (self.__class__.__name__, self.name, - str(self.bbox), self.srcsize)) \ No newline at end of file + str(self.bbox), self.srcsize)) + + def save_image(self, outdir: str, file_name: str = None): + stream = self.stream + filters = stream.get_filters() + width = self.bbox.width + height = self.bbox.height + bits = self.bits + + if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE: + ext = '.jpg' + elif (bits == 1 or + bits == 8 and self.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)): + ext = '.w%dh%d.bmp' % (width, height) + else: + ext = '.b%dw%dh%d.img' % (bits, width, height) + + if file_name is None: + file_name = self.name + ext + else: + file_name += ext + path = os.path.join(outdir, file_name) + + with open(path, 'wb') as fp: + if ext == '.jpg': + raw_data = stream.get_rawdata() + if LITERAL_DEVICE_CMYK in self.colorspace: + ifp = BytesIO(raw_data) + i = pillow.Image.open(ifp) + i = pillow.ImageChops.invert(i) + i = i.convert('RGB') + i.save(fp, 'JPEG') + else: + fp.write(raw_data) + elif bits == 1: + BMPWriter.write_all(fp, stream, 1, width, height, (width+7)//8) + elif bits == 8 and self.colorspace is LITERAL_DEVICE_RGB: + BMPWriter.write_all(fp, stream, 24, width, height, width*3) + elif bits == 8 and self.colorspace is LITERAL_DEVICE_GRAY: + BMPWriter.write_all(fp, stream, 8, width, height, width) + else: + fp.write(stream.get_data()) + return path \ No newline at end of file diff --git a/pdfmajor/parser/PDFColorSpace.py b/pdfmajor/parser/PDFColorSpace.py deleted file mode 100644 index 0ce2482..0000000 --- a/pdfmajor/parser/PDFColorSpace.py +++ /dev/null @@ -1,35 +0,0 @@ -import collections -from .PSStackParser import LIT - -## PDFColorSpace -## -LITERAL_DEVICE_GRAY = LIT('DeviceGray') -LITERAL_DEVICE_RGB = LIT('DeviceRGB') -LITERAL_DEVICE_CMYK = LIT('DeviceCMYK') - - -class PDFColorSpace(object): - - def __init__(self, name: str, ncomponents: int): - self.name: str = name - self.ncomponents: int = ncomponents - return - - def __repr__(self): - return '' % (self.name, self.ncomponents) - - -PREDEFINED_COLORSPACE = collections.OrderedDict() - -for (name, n) in [ - ('DeviceGray', 1), # default value first - ('CalRGB', 3), - ('CalGray', 1), - ('Lab', 3), - ('DeviceRGB', 3), - ('DeviceCMYK', 4), - ('Separation', 1), - ('Indexed', 1), - ('Pattern', 1), -]: - PREDEFINED_COLORSPACE[name]=PDFColorSpace(name, n) diff --git a/tests/samples/pdf/jpg.pdf b/tests/samples/pdf/jpg.pdf new file mode 100644 index 0000000..23e01c2 Binary files /dev/null and b/tests/samples/pdf/jpg.pdf differ diff --git a/tests/samples/pdf/long-tables.pdf b/tests/samples/pdf/long-tables.pdf deleted file mode 100644 index bc04482..0000000 Binary files a/tests/samples/pdf/long-tables.pdf and /dev/null differ diff --git a/tests/samples/pdf/looseless.pdf b/tests/samples/pdf/looseless.pdf new file mode 100644 index 0000000..8b40107 Binary files /dev/null and b/tests/samples/pdf/looseless.pdf differ diff --git a/tests/test_image_writer.py b/tests/test_image_writer.py new file mode 100644 index 0000000..18b1de2 --- /dev/null +++ b/tests/test_image_writer.py @@ -0,0 +1,33 @@ +import os + +from pdfmajor.interpreter import LTImage, logging +from pdfmajor.interpreter import PageInterpreter, PDFInterpreter +from pdfmajor.interpreter.commands import LTItem +from pdfmajor.interpreter.commands import LTCharBlock, LTChar + +CUR_PATH = os.path.dirname(os.path.dirname(__file__)) +FILES = [ + "jpg", + "looseless" +] +OUTPUT_FOLDER = os.path.join( + CUR_PATH, + 'samples/output/images' +) + +if not os.path.exists(OUTPUT_FOLDER): + os.makedirs(OUTPUT_FOLDER) + +for file_name in FILES: + for page in PDFInterpreter(os.path.join( + CUR_PATH, + f"samples/pdf/{file_name}.pdf" + ), debug_level=logging.ERROR): + assert isinstance(page, PageInterpreter) + for item_idx, item in enumerate(page): + assert isinstance(item, LTItem) + if isinstance(item, LTImage): + print(item.save_image( + OUTPUT_FOLDER, + file_name=f'{file_name}_p{page.page_num}i{item_idx}' + )) \ No newline at end of file