- image extraction

This commit is contained in:
ari committed 2019-01-04 15:43:07 -05:00
1 parent d1d1b23f2f
commit ef230847ac
9 files changed
+167 -172

No files matched your search

+60
View File
@@ -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('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, str(len(header))
self.fp.write(header)
self.fp.write(info)
if ncols == 2:
# B&W color table
for i in (0, 255):
self.fp.write(struct.pack('BBBx', i, i, i))
elif ncols == 256:
# grayscale color table
for i in range(256):
self.fp.write(struct.pack('BBBx', i, i, i))
self.pos0 = self.fp.tell()
self.pos1 = self.pos0 + self.datasize
return
def write_line(self, y, data):
self.fp.seek(self.pos1 - (y+1)*self.linesize)
self.fp.write(data)
return
-127
View File
@@ -1,127 +0,0 @@
import struct
import os
import os.path
from io import BytesIO
from ..interpreter.PDFStream import LITERALS_DCT_DECODE
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_GRAY
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_RGB
from ..interpreter.PDFColorSpace import LITERAL_DEVICE_CMYK
from ..layouts import LTImage
def align32(x):
return ((x+3)//4)*4
## BMPWriter
##
class BMPWriter(object):
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('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
assert len(info) == 40, str(len(info))
header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
assert len(header) == 14, str(len(header))
self.fp.write(header)
self.fp.write(info)
if ncols == 2:
# B&W color table
for i in (0, 255):
self.fp.write(struct.pack('BBBx', i, i, i))
elif ncols == 256:
# grayscale color table
for i in range(256):
self.fp.write(struct.pack('BBBx', i, i, i))
self.pos0 = self.fp.tell()
self.pos1 = self.pos0 + self.datasize
return
def write_line(self, y, data):
self.fp.seek(self.pos1 - (y+1)*self.linesize)
self.fp.write(data)
return
## ImageWriter
##
class ImageWriter(object):
def __init__(self, outdir: str = None):
if outdir is None:
self.inactive = True
else:
self.inactive = False
self.outdir = outdir
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
def export_image(self, image: LTImage) -> 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
+18 -8
View File
@@ -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
@@ -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))
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
-35
View File
@@ -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 '<PDFColorSpace: %s, ncomponents=%d>' % (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)
Binary file not shown.
Binary file not shown.
Binary file not shown.
+33
View File
@@ -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}'
))