Skip to content

Simplify pdf image output. #15175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,14 +1412,14 @@ def imageObject(self, image):

def _unpack(self, im):
"""
Unpack the image object im into height, width, data, alpha,
where data and alpha are HxWx3 (RGB) or HxWx1 (grayscale or alpha)
arrays, except alpha is None if the image is fully opaque.
Unpack image array *im* into ``(data, alpha)``, which have shape
``(height, width, 3)`` (RGB) or ``(height, width, 1)`` (grayscale or
alpha), except that alpha is None if the image is fully opaque.
"""
h, w = im.shape[:2]
im = im[::-1]
if im.ndim == 2:
return h, w, im, None
return im, None
else:
rgb = im[:, :, :3]
rgb = np.array(rgb, order='C')
Expand All @@ -1432,7 +1432,7 @@ def _unpack(self, im):
alpha = np.array(alpha, order='C')
else:
alpha = None
return h, w, rgb, alpha
return rgb, alpha

def _writePng(self, data):
"""
Expand All @@ -1455,27 +1455,24 @@ def _writePng(self, data):
buffer.seek(length, 1)
buffer.seek(4, 1) # skip CRC

def _writeImg(self, data, height, width, grayscale, id, smask=None):
def _writeImg(self, data, id, smask=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we ever actually pass in a grayscale image?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do pass single-channel images (for alpha). As for actual grayscale I don't know (see #12871 for the similar thing in postscript), but this PR doesn't change that behavior anyways.

"""
Write the image *data* of size *height* x *width*, as grayscale
if *grayscale* is true and RGB otherwise, as pdf object *id*
and with the soft mask (alpha channel) *smask*, which should be
either None or a *height* x *width* x 1 array.
Write the image *data*, of shape ``(height, width, 1)`` (grayscale) or
``(height, width, 3)`` (RGB), as pdf object *id* and with the soft mask
(alpha channel) *smask*, which should be either None or a ``(height,
width, 1)`` array.
"""

obj = {'Type': Name('XObject'),
'Subtype': Name('Image'),
'Width': width,
'Height': height,
'ColorSpace': Name('DeviceGray' if grayscale
else 'DeviceRGB'),
height, width, colors = data.shape
obj = {'Type': Name('XObject'),
'Subtype': Name('Image'),
'Width': width,
'Height': height,
'ColorSpace': Name({1: 'DeviceGray', 3: 'DeviceRGB'}[colors]),
'BitsPerComponent': 8}
if smask:
obj['SMask'] = smask
if rcParams['pdf.compression']:
png = {'Predictor': 10,
'Colors': 1 if grayscale else 3,
'Columns': width}
png = {'Predictor': 10, 'Colors': colors, 'Columns': width}
else:
png = None
self.beginStream(
Expand All @@ -1492,14 +1489,13 @@ def _writeImg(self, data, height, width, grayscale, id, smask=None):

def writeImages(self):
for img, name, ob in self._images.values():
height, width, data, adata = self._unpack(img)
data, adata = self._unpack(img)
if adata is not None:
smaskObject = self.reserveObject("smask")
self._writeImg(adata, height, width, True, smaskObject.id)
self._writeImg(adata, smaskObject.id)
else:
smaskObject = None
self._writeImg(data, height, width, False,
ob.id, smaskObject)
self._writeImg(data, ob.id, smaskObject)

def markerObject(self, path, trans, fill, stroke, lw, joinstyle,
capstyle):
Expand Down