Skip to content

Commit efc4996

Browse files
committed
Simplify pdf image output.
Let _writeImg infer image size and grayscaleness from the data itself.
1 parent d576e3a commit efc4996

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lib/matplotlib/backends/backend_pdf.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -1412,14 +1412,14 @@ def imageObject(self, image):
14121412

14131413
def _unpack(self, im):
14141414
"""
1415-
Unpack the image object im into height, width, data, alpha,
1416-
where data and alpha are HxWx3 (RGB) or HxWx1 (grayscale or alpha)
1417-
arrays, except alpha is None if the image is fully opaque.
1415+
Unpack image array *img* into ``(data, alpha)``, which have shape
1416+
``(height, width, 3)`` (RGB) or ``(height, width, 1)`` (grayscale or
1417+
alpha), except that alpha is None if the image is fully opaque.
14181418
"""
14191419
h, w = im.shape[:2]
14201420
im = im[::-1]
14211421
if im.ndim == 2:
1422-
return h, w, im, None
1422+
return im, None
14231423
else:
14241424
rgb = im[:, :, :3]
14251425
rgb = np.array(rgb, order='C')
@@ -1432,7 +1432,7 @@ def _unpack(self, im):
14321432
alpha = np.array(alpha, order='C')
14331433
else:
14341434
alpha = None
1435-
return h, w, rgb, alpha
1435+
return rgb, alpha
14361436

14371437
def _writePng(self, data):
14381438
"""
@@ -1455,27 +1455,24 @@ def _writePng(self, data):
14551455
buffer.seek(length, 1)
14561456
buffer.seek(4, 1) # skip CRC
14571457

1458-
def _writeImg(self, data, height, width, grayscale, id, smask=None):
1458+
def _writeImg(self, data, id, smask=None):
14591459
"""
1460-
Write the image *data* of size *height* x *width*, as grayscale
1461-
if *grayscale* is true and RGB otherwise, as pdf object *id*
1462-
and with the soft mask (alpha channel) *smask*, which should be
1463-
either None or a *height* x *width* x 1 array.
1460+
Write the image *data*, of shape ``(height, width, 1)`` (grayscale) or
1461+
``(height, width, 3)`` (RGB), as pdf object *id* and with the soft mask
1462+
(alpha channel) *smask*, which should be either None or a ``(height,
1463+
width, 1)`` array.
14641464
"""
1465-
1466-
obj = {'Type': Name('XObject'),
1467-
'Subtype': Name('Image'),
1468-
'Width': width,
1469-
'Height': height,
1470-
'ColorSpace': Name('DeviceGray' if grayscale
1471-
else 'DeviceRGB'),
1465+
height, width, colors = data.shape
1466+
obj = {'Type': Name('XObject'),
1467+
'Subtype': Name('Image'),
1468+
'Width': width,
1469+
'Height': height,
1470+
'ColorSpace': Name({1: 'DeviceGray', 3: 'DeviceRGB'}[colors]),
14721471
'BitsPerComponent': 8}
14731472
if smask:
14741473
obj['SMask'] = smask
14751474
if rcParams['pdf.compression']:
1476-
png = {'Predictor': 10,
1477-
'Colors': 1 if grayscale else 3,
1478-
'Columns': width}
1475+
png = {'Predictor': 10, 'Colors': colors, 'Columns': width}
14791476
else:
14801477
png = None
14811478
self.beginStream(
@@ -1492,14 +1489,13 @@ def _writeImg(self, data, height, width, grayscale, id, smask=None):
14921489

14931490
def writeImages(self):
14941491
for img, name, ob in self._images.values():
1495-
height, width, data, adata = self._unpack(img)
1492+
data, adata = self._unpack(img)
14961493
if adata is not None:
14971494
smaskObject = self.reserveObject("smask")
1498-
self._writeImg(adata, height, width, True, smaskObject.id)
1495+
self._writeImg(adata, smaskObject.id)
14991496
else:
15001497
smaskObject = None
1501-
self._writeImg(data, height, width, False,
1502-
ob.id, smaskObject)
1498+
self._writeImg(data, ob.id, smaskObject)
15031499

15041500
def markerObject(self, path, trans, fill, stroke, lw, joinstyle,
15051501
capstyle):

0 commit comments

Comments
 (0)