@@ -1261,34 +1261,32 @@ def imageObject(self, image):
12611261 self .images [image ] = (name , ob )
12621262 return name
12631263
1264- def _rgb (self , im ):
1265- h , w , s = im .as_rgba_str ()
1264+ def _unpack (self , im ):
1265+ """Unpack the image object im into height, width, data, alpha,
1266+ where data and alpha are HxWx3 (RGB) or HxWx1 (grayscale or alpha)
1267+ arrays, except alpha is None if the image is fully opaque."""
12661268
1269+ h , w , s = im .as_rgba_str ()
12671270 rgba = np .fromstring (s , np .uint8 )
12681271 rgba .shape = (h , w , 4 )
12691272 rgba = rgba [::- 1 ]
1270- rgb = np .ascontiguousarray (rgba [:, :, :3 ])
1271- alpha = np .ascontiguousarray (rgba [:, :, 3 ][..., None ])
1272- if np .all (alpha == 255 ):
1273- alpha = None
1274- return h , w , rgb , alpha
1275-
1276- def _gray (self , im , rc = 0.3 , gc = 0.59 , bc = 0.11 ):
1277- rgbat = im .as_rgba_str ()
1278- rgba = np .fromstring (rgbat [2 ], np .uint8 )
1279- rgba .shape = (rgbat [0 ], rgbat [1 ], 4 )
1280- rgba = rgba [::- 1 ]
1281- rgba_f = rgba .astype (np .float32 )
1282- r = rgba_f [:, :, 0 ]
1283- g = rgba_f [:, :, 1 ]
1284- b = rgba_f [:, :, 2 ]
1285- alpha = np .ascontiguousarray (rgba [:, :, 3 ][..., None ])
1273+ rgb = rgba [:, :, :3 ]
1274+ alpha = rgba [:, :, 3 ][..., None ]
12861275 if np .all (alpha == 255 ):
12871276 alpha = None
1288- gray = (r * rc + g * gc + b * bc ).astype (np .uint8 )[..., None ]
1289- return rgbat [0 ], rgbat [1 ], gray , alpha
1277+ else :
1278+ alpha = np .array (alpha , order = 'C' )
1279+ if im .is_grayscale :
1280+ r , g , b = rgb .astype (np .float32 ).transpose (2 , 0 , 1 )
1281+ gray = (0.3 * r + 0.59 * g + 0.11 * b ).astype (np .uint8 )[..., None ]
1282+ return h , w , gray , alpha
1283+ else :
1284+ rgb = np .array (rgb , order = 'C' )
1285+ return h , w , rgb , alpha
12901286
12911287 def _writePng (self , data ):
1288+ """Write the image *data* into the pdf file using png
1289+ predictors with Flate compression."""
12921290 buffer = BytesIO ()
12931291 _png .write_png (data , buffer )
12941292 buffer .seek (8 )
@@ -1311,6 +1309,9 @@ def _writePng(self, data):
13111309 buffer .seek (4 , 1 ) # skip CRC
13121310
13131311 def _writeImg (self , data , height , width , grayscale , id , smask = None ):
1312+ """Write the image *data* of size *height* x *width*, as grayscale
1313+ if *grayscale* is true and RGB otherwise, as pdf object *id*
1314+ and with the soft mask (alpha channel) *smask*."""
13141315 obj = {'Type' : Name ('XObject' ),
13151316 'Subtype' : Name ('Image' ),
13161317 'Width' : width ,
@@ -1340,11 +1341,7 @@ def _writeImg(self, data, height, width, grayscale, id, smask=None):
13401341
13411342 def writeImages (self ):
13421343 for img , pair in six .iteritems (self .images ):
1343- if img .is_grayscale :
1344- height , width , data , adata = self ._gray (img )
1345- else :
1346- height , width , data , adata = self ._rgb (img )
1347-
1344+ height , width , data , adata = self ._unpack (img )
13481345 if adata is not None :
13491346 smaskObject = self .reserveObject ("smask" )
13501347 self ._writeImg (adata , height , width , True , smaskObject .id )
0 commit comments