2014-06-26 09:12:39 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2019-10-22 15:37:06 +00:00
|
|
|
import struct
|
2014-06-25 10:55:41 +00:00
|
|
|
from io import BytesIO
|
2019-10-22 15:37:06 +00:00
|
|
|
|
|
|
|
from .jbig2 import JBIG2StreamReader, JBIG2StreamWriter
|
|
|
|
from .pdfcolor import LITERAL_DEVICE_CMYK
|
2014-06-26 09:12:39 +00:00
|
|
|
from .pdfcolor import LITERAL_DEVICE_GRAY
|
|
|
|
from .pdfcolor import LITERAL_DEVICE_RGB
|
2019-10-22 15:37:06 +00:00
|
|
|
from .pdftypes import LITERALS_DCT_DECODE, LITERALS_JBIG2_DECODE
|
2011-07-18 11:06:50 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2011-07-18 11:06:50 +00:00
|
|
|
def align32(x):
|
2013-11-26 12:35:16 +00:00
|
|
|
return ((x+3)//4)*4
|
2011-07-18 11:06:50 +00:00
|
|
|
|
2013-11-07 08:35:04 +00:00
|
|
|
|
2020-01-04 15:47:07 +00:00
|
|
|
class BMPWriter:
|
2011-07-18 11:06:50 +00:00
|
|
|
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)
|
2013-11-26 12:35:16 +00:00
|
|
|
self.linesize = align32((self.width*self.bits+7)//8)
|
2011-07-18 11:06:50 +00:00
|
|
|
self.datasize = self.linesize * self.height
|
2011-11-07 14:29:24 +00:00
|
|
|
headersize = 14+40+ncols*4
|
2019-12-29 20:20:20 +00:00
|
|
|
info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height,
|
|
|
|
1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
|
2017-05-29 07:06:09 +00:00
|
|
|
assert len(info) == 40, str(len(info))
|
2019-12-29 20:20:20 +00:00
|
|
|
header = struct.pack('<ccIHHI', b'B', b'M',
|
|
|
|
headersize+self.datasize, 0, 0, headersize)
|
2017-05-29 07:06:09 +00:00
|
|
|
assert len(header) == 14, str(len(header))
|
2011-07-18 11:06:50 +00:00
|
|
|
self.fp.write(header)
|
|
|
|
self.fp.write(info)
|
|
|
|
if ncols == 2:
|
2011-11-07 14:29:24 +00:00
|
|
|
# B&W color table
|
2013-11-07 08:35:04 +00:00
|
|
|
for i in (0, 255):
|
|
|
|
self.fp.write(struct.pack('BBBx', i, i, i))
|
2011-07-18 11:06:50 +00:00
|
|
|
elif ncols == 256:
|
2011-11-07 14:29:24 +00:00
|
|
|
# grayscale color table
|
2014-09-16 21:01:16 +00:00
|
|
|
for i in range(256):
|
2013-11-07 08:35:04 +00:00
|
|
|
self.fp.write(struct.pack('BBBx', i, i, i))
|
2011-07-18 11:06:50 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-01-04 15:47:07 +00:00
|
|
|
class ImageWriter:
|
2019-10-22 15:37:06 +00:00
|
|
|
"""Write image to a file
|
|
|
|
|
|
|
|
Supports various image types: JPEG, JBIG2 and bitmaps
|
|
|
|
"""
|
2011-07-18 11:06:50 +00:00
|
|
|
|
|
|
|
def __init__(self, outdir):
|
|
|
|
self.outdir = outdir
|
2011-11-06 15:15:10 +00:00
|
|
|
if not os.path.exists(self.outdir):
|
|
|
|
os.makedirs(self.outdir)
|
2011-07-18 11:06:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def export_image(self, image):
|
|
|
|
(width, height) = image.srcsize
|
2019-10-22 15:37:06 +00:00
|
|
|
|
|
|
|
is_jbig2 = self.is_jbig2_image(image)
|
|
|
|
ext = self._get_image_extension(image, width, height, is_jbig2)
|
2019-12-29 20:20:20 +00:00
|
|
|
name, path = self._create_unique_image_name(self.outdir,
|
|
|
|
image.name, ext)
|
2019-10-22 15:37:06 +00:00
|
|
|
|
|
|
|
fp = open(path, 'wb')
|
2011-07-18 11:06:50 +00:00
|
|
|
if ext == '.jpg':
|
2019-10-22 15:37:06 +00:00
|
|
|
raw_data = image.stream.get_rawdata()
|
2012-01-25 00:18:36 +00:00
|
|
|
if LITERAL_DEVICE_CMYK in image.colorspace:
|
2013-04-09 09:42:32 +00:00
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageChops
|
2014-06-25 10:55:41 +00:00
|
|
|
ifp = BytesIO(raw_data)
|
2013-11-07 07:14:53 +00:00
|
|
|
i = Image.open(ifp)
|
2012-01-25 00:18:36 +00:00
|
|
|
i = ImageChops.invert(i)
|
|
|
|
i = i.convert('RGB')
|
|
|
|
i.save(fp, 'JPEG')
|
|
|
|
else:
|
|
|
|
fp.write(raw_data)
|
2019-10-22 15:37:06 +00:00
|
|
|
elif is_jbig2:
|
|
|
|
input_stream = BytesIO()
|
|
|
|
input_stream.write(image.stream.get_data())
|
|
|
|
input_stream.seek(0)
|
|
|
|
reader = JBIG2StreamReader(input_stream)
|
|
|
|
segments = reader.get_segments()
|
|
|
|
|
|
|
|
writer = JBIG2StreamWriter(fp)
|
|
|
|
writer.write_file(segments)
|
2011-07-18 11:06:50 +00:00
|
|
|
elif image.bits == 1:
|
|
|
|
bmp = BMPWriter(fp, 1, width, height)
|
2019-10-22 15:37:06 +00:00
|
|
|
data = image.stream.get_data()
|
2011-07-18 11:06:50 +00:00
|
|
|
i = 0
|
2013-11-26 12:35:16 +00:00
|
|
|
width = (width+7)//8
|
2014-09-16 21:01:16 +00:00
|
|
|
for y in range(height):
|
2011-07-18 11:06:50 +00:00
|
|
|
bmp.write_line(y, data[i:i+width])
|
|
|
|
i += width
|
2019-10-15 14:11:54 +00:00
|
|
|
elif image.bits == 8 and LITERAL_DEVICE_RGB in image.colorspace:
|
2011-07-18 11:06:50 +00:00
|
|
|
bmp = BMPWriter(fp, 24, width, height)
|
2019-10-22 15:37:06 +00:00
|
|
|
data = image.stream.get_data()
|
2011-07-18 11:06:50 +00:00
|
|
|
i = 0
|
|
|
|
width = width*3
|
2014-09-16 21:01:16 +00:00
|
|
|
for y in range(height):
|
2011-07-18 11:06:50 +00:00
|
|
|
bmp.write_line(y, data[i:i+width])
|
|
|
|
i += width
|
2019-10-15 14:11:54 +00:00
|
|
|
elif image.bits == 8 and LITERAL_DEVICE_GRAY in image.colorspace:
|
2011-07-18 11:06:50 +00:00
|
|
|
bmp = BMPWriter(fp, 8, width, height)
|
2019-10-22 15:37:06 +00:00
|
|
|
data = image.stream.get_data()
|
2011-07-18 11:06:50 +00:00
|
|
|
i = 0
|
2014-09-16 21:01:16 +00:00
|
|
|
for y in range(height):
|
2011-07-18 11:06:50 +00:00
|
|
|
bmp.write_line(y, data[i:i+width])
|
|
|
|
i += width
|
|
|
|
else:
|
2019-10-22 15:37:06 +00:00
|
|
|
fp.write(image.stream.get_data())
|
2011-07-18 11:06:50 +00:00
|
|
|
fp.close()
|
|
|
|
return name
|
2019-10-22 15:37:06 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_jbig2_image(image):
|
|
|
|
filters = image.stream.get_filters()
|
|
|
|
is_jbig2 = False
|
|
|
|
for filter_name, params in filters:
|
|
|
|
if filter_name in LITERALS_JBIG2_DECODE:
|
|
|
|
is_jbig2 = True
|
|
|
|
break
|
|
|
|
return is_jbig2
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_image_extension(image, width, height, is_jbig2):
|
|
|
|
filters = image.stream.get_filters()
|
|
|
|
if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE:
|
|
|
|
ext = '.jpg'
|
|
|
|
elif is_jbig2:
|
|
|
|
ext = '.jb2'
|
|
|
|
elif (image.bits == 1 or
|
2019-12-29 20:20:20 +00:00
|
|
|
image.bits == 8 and
|
|
|
|
(LITERAL_DEVICE_RGB in image.colorspace or
|
|
|
|
LITERAL_DEVICE_GRAY in image.colorspace)):
|
2019-10-22 15:37:06 +00:00
|
|
|
ext = '.%dx%d.bmp' % (width, height)
|
|
|
|
else:
|
|
|
|
ext = '.%d.%dx%d.img' % (image.bits, width, height)
|
|
|
|
return ext
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _create_unique_image_name(dirname, image_name, ext):
|
|
|
|
name = image_name + ext
|
|
|
|
path = os.path.join(dirname, name)
|
|
|
|
img_index = 0
|
|
|
|
while os.path.exists(path):
|
|
|
|
name = '%s.%d%s' % (image_name, img_index, ext)
|
|
|
|
path = os.path.join(dirname, name)
|
|
|
|
img_index += 1
|
|
|
|
return name, path
|