@@ -1557,12 +1557,13 @@ def _writeImg(self, data, id, smask=None):
1557
1557
(alpha channel) *smask*, which should be either None or a ``(height,
1558
1558
width, 1)`` array.
1559
1559
"""
1560
- height , width , colors = data .shape
1560
+ height , width , color_channels = data .shape
1561
1561
obj = {'Type' : Name ('XObject' ),
1562
1562
'Subtype' : Name ('Image' ),
1563
1563
'Width' : width ,
1564
1564
'Height' : height ,
1565
- 'ColorSpace' : Name ({1 : 'DeviceGray' , 3 : 'DeviceRGB' }[colors ]),
1565
+ 'ColorSpace' : Name ({1 : 'DeviceGray' ,
1566
+ 3 : 'DeviceRGB' }[color_channels ]),
1566
1567
'BitsPerComponent' : 8 }
1567
1568
if smask :
1568
1569
obj ['SMask' ] = smask
@@ -1571,13 +1572,13 @@ def _writeImg(self, data, id, smask=None):
1571
1572
data = data .squeeze (axis = - 1 )
1572
1573
img = Image .fromarray (data )
1573
1574
img_colors = img .getcolors (maxcolors = 256 )
1574
- if colors == 3 and img_colors is not None :
1575
+ if color_channels == 3 and img_colors is not None :
1575
1576
# Convert to indexed color if there are 256 colors or fewer
1576
1577
# This can significantly reduce the file size
1577
1578
num_colors = len (img_colors )
1578
1579
img = img .convert (mode = 'P' , dither = Image .NONE ,
1579
1580
palette = Image .ADAPTIVE , colors = num_colors )
1580
- data , bit_depth , palette = self ._writePng (img )
1581
+ png_data , bit_depth , palette = self ._writePng (img )
1581
1582
if bit_depth is None or palette is None :
1582
1583
raise RuntimeError ("invalid PNG header" )
1583
1584
palette = palette [:num_colors * 3 ] # Trim padding
@@ -1586,10 +1587,10 @@ def _writeImg(self, data, id, smask=None):
1586
1587
+ str (num_colors - 1 ).encode ()
1587
1588
+ b' ' + palette + b']' )
1588
1589
obj ['BitsPerComponent' ] = bit_depth
1589
- colors = 1
1590
+ color_channels = 1
1590
1591
else :
1591
- data , _ , _ = self ._writePng (img )
1592
- png = {'Predictor' : 10 , 'Colors' : colors , 'Columns' : width }
1592
+ png_data , _ , _ = self ._writePng (img )
1593
+ png = {'Predictor' : 10 , 'Colors' : color_channels , 'Columns' : width }
1593
1594
else :
1594
1595
png = None
1595
1596
self .beginStream (
@@ -1599,7 +1600,7 @@ def _writeImg(self, data, id, smask=None):
1599
1600
png = png
1600
1601
)
1601
1602
if png :
1602
- self .currentstream .write (data )
1603
+ self .currentstream .write (png_data )
1603
1604
else :
1604
1605
self .currentstream .write (data .tobytes ())
1605
1606
self .endStream ()
0 commit comments