@@ -1412,14 +1412,14 @@ def imageObject(self, image):
1412
1412
1413
1413
def _unpack (self , im ):
1414
1414
"""
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.
1418
1418
"""
1419
1419
h , w = im .shape [:2 ]
1420
1420
im = im [::- 1 ]
1421
1421
if im .ndim == 2 :
1422
- return h , w , im , None
1422
+ return im , None
1423
1423
else :
1424
1424
rgb = im [:, :, :3 ]
1425
1425
rgb = np .array (rgb , order = 'C' )
@@ -1432,7 +1432,7 @@ def _unpack(self, im):
1432
1432
alpha = np .array (alpha , order = 'C' )
1433
1433
else :
1434
1434
alpha = None
1435
- return h , w , rgb , alpha
1435
+ return rgb , alpha
1436
1436
1437
1437
def _writePng (self , data ):
1438
1438
"""
@@ -1455,27 +1455,24 @@ def _writePng(self, data):
1455
1455
buffer .seek (length , 1 )
1456
1456
buffer .seek (4 , 1 ) # skip CRC
1457
1457
1458
- def _writeImg (self , data , height , width , grayscale , id , smask = None ):
1458
+ def _writeImg (self , data , id , smask = None ):
1459
1459
"""
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.
1464
1464
"""
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 ]),
1472
1471
'BitsPerComponent' : 8 }
1473
1472
if smask :
1474
1473
obj ['SMask' ] = smask
1475
1474
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 }
1479
1476
else :
1480
1477
png = None
1481
1478
self .beginStream (
@@ -1492,14 +1489,13 @@ def _writeImg(self, data, height, width, grayscale, id, smask=None):
1492
1489
1493
1490
def writeImages (self ):
1494
1491
for img , name , ob in self ._images .values ():
1495
- height , width , data , adata = self ._unpack (img )
1492
+ data , adata = self ._unpack (img )
1496
1493
if adata is not None :
1497
1494
smaskObject = self .reserveObject ("smask" )
1498
- self ._writeImg (adata , height , width , True , smaskObject .id )
1495
+ self ._writeImg (adata , smaskObject .id )
1499
1496
else :
1500
1497
smaskObject = None
1501
- self ._writeImg (data , height , width , False ,
1502
- ob .id , smaskObject )
1498
+ self ._writeImg (data , ob .id , smaskObject )
1503
1499
1504
1500
def markerObject (self , path , trans , fill , stroke , lw , joinstyle ,
1505
1501
capstyle ):
0 commit comments