@@ -10097,7 +10097,10 @@ def color_topusage(self, clip=None):
10097
10097
@property
10098
10098
def colorspace(self):
10099
10099
"""Pixmap Colorspace."""
10100
- return Colorspace(mupdf.fz_pixmap_colorspace(self.this))
10100
+ cs = Colorspace(mupdf.fz_pixmap_colorspace(self.this))
10101
+ if cs.name == "None":
10102
+ return None
10103
+ return cs
10101
10104
10102
10105
def copy(self, src, bbox):
10103
10106
"""Copy bbox from another Pixmap."""
@@ -10227,44 +10230,57 @@ def pdfocr_tobytes(self, compress=True, language="eng", tessdata=None):
10227
10230
self.pdfocr_save(bio, compress=compress, language=language, tessdata=tessdata)
10228
10231
return bio.getvalue()
10229
10232
10230
- def pil_save(self, *args, **kwargs):
10231
- """Write to image file using Pillow.
10232
-
10233
- Args are passed to Pillow's Image.save method, see their documentation.
10234
- Use instead of save when other output formats are desired.
10235
- """
10233
+ def pil_image(self):
10234
+ """Create a Pillow Image from the Pixmap."""
10236
10235
try:
10237
10236
from PIL import Image
10238
10237
except ImportError:
10239
10238
message("PIL/Pillow not installed")
10240
10239
raise
10241
10240
10242
10241
cspace = self.colorspace
10243
- if cspace is None :
10242
+ if not cspace :
10244
10243
mode = "L"
10245
10244
elif cspace.n == 1:
10246
- mode = "L" if self.alpha == 0 else "LA"
10245
+ mode = "L" if not self.alpha else "LA"
10247
10246
elif cspace.n == 3:
10248
- mode = "RGB" if self.alpha == 0 else "RGBA"
10247
+ mode = "RGB" if not self.alpha else "RGBA"
10249
10248
else:
10250
10249
mode = "CMYK"
10251
10250
10252
10251
img = Image.frombytes(mode, (self.width, self.height), self.samples)
10252
+ return img
10253
+
10254
+ def pil_save(self, *args, **kwargs):
10255
+ """Write to image file using Pillow.
10256
+
10257
+ An intermediate PIL Image is created, and its "save" method is used
10258
+ to store the image. See Pillow documentation to learn about the
10259
+ meaning of possible positional and keyword parameters.
10260
+ Use this when other output formats are desired.
10261
+ """
10262
+ img = self.pil_image()
10253
10263
10254
10264
if "dpi" not in kwargs.keys():
10255
10265
kwargs["dpi"] = (self.xres, self.yres)
10256
10266
10257
10267
img.save(*args, **kwargs)
10258
10268
10259
10269
def pil_tobytes(self, *args, **kwargs):
10260
- """Convert to binary image stream using pillow .
10270
+ """Convert to an image in memory using Pillow .
10261
10271
10262
- Args are passed to Pillow's Image.save method, see their documentation.
10263
- Use instead of 'tobytes' when other output formats are needed.
10272
+ An intermediate PIL Image is created, and its "save" method is used
10273
+ to store the image. See Pillow documentation to learn about the
10274
+ meaning of possible positional or keyword parameters.
10275
+ Use this when other output formats are desired.
10264
10276
"""
10265
- from io import BytesIO
10266
- bytes_out = BytesIO()
10267
- self.pil_save(bytes_out, *args, **kwargs)
10277
+ bytes_out = io.BytesIO()
10278
+ img = self.pil_image()
10279
+
10280
+ if "dpi" not in kwargs.keys():
10281
+ kwargs["dpi"] = (self.xres, self.yres)
10282
+
10283
+ img.save(bytes_out, *args, **kwargs)
10268
10284
return bytes_out.getvalue()
10269
10285
10270
10286
def pixel(self, x, y):
0 commit comments