diff --git a/docs/pixmap.rst b/docs/pixmap.rst index a6546618b..3ffbb0c7f 100644 --- a/docs/pixmap.rst +++ b/docs/pixmap.rst @@ -33,8 +33,9 @@ Have a look at the :ref:`FAQ` section to see some pixmap usage "at work". :meth:`Pixmap.invert_irect` invert the pixels of a given area :meth:`Pixmap.pdfocr_save` save the pixmap as an OCRed 1-page PDF :meth:`Pixmap.pdfocr_tobytes` save the pixmap as an OCRed 1-page PDF -:meth:`Pixmap.pil_save` save as image using pillow -:meth:`Pixmap.pil_tobytes` write to `bytes` object using pillow +:meth:`Pixmap.pil_image` create a Pillow Image +:meth:`Pixmap.pil_save` save as a Pillow Image +:meth:`Pixmap.pil_tobytes` write to `bytes` as a Pillow Image :meth:`Pixmap.pixel` return the value of a pixel :meth:`Pixmap.save` save the pixmap in a variety of formats :meth:`Pixmap.set_alpha` set alpha values @@ -388,9 +389,14 @@ Have a look at the :ref:`FAQ` section to see some pixmap usage "at work". doc.save("ocr-images.pdf") - .. method:: pil_save(*args, unmultiply=False, **kwargs) + .. method:: pil_image() - * New in v1.17.3 + Create a Pillow Image from the pixmap. PIL / Pillow must be installed. + + :raises ImportError: if Pillow is not installed. + :returns: a ˇˇPIL.Imageˇˇ object + + .. method:: pil_save(*args, unmultiply=False, **kwargs) Write the pixmap as an image file using Pillow. Use this method for output unsupported by MuPDF. Examples are diff --git a/src/__init__.py b/src/__init__.py index 727d176c3..ef6daa6e0 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -10097,7 +10097,10 @@ def color_topusage(self, clip=None): @property def colorspace(self): """Pixmap Colorspace.""" - return Colorspace(mupdf.fz_pixmap_colorspace(self.this)) + cs = Colorspace(mupdf.fz_pixmap_colorspace(self.this)) + if repr(cs).endswith("None"): + return None + return cs def copy(self, src, bbox): """Copy bbox from another Pixmap.""" @@ -10227,12 +10230,8 @@ def pdfocr_tobytes(self, compress=True, language="eng", tessdata=None): self.pdfocr_save(bio, compress=compress, language=language, tessdata=tessdata) return bio.getvalue() - def pil_save(self, *args, **kwargs): - """Write to image file using Pillow. - - Args are passed to Pillow's Image.save method, see their documentation. - Use instead of save when other output formats are desired. - """ + def pil_image(self): + """Create a Pillow Image from the Pixmap.""" try: from PIL import Image except ImportError: @@ -10240,16 +10239,27 @@ def pil_save(self, *args, **kwargs): raise cspace = self.colorspace - if cspace is None: + if not cspace: mode = "L" elif cspace.n == 1: - mode = "L" if self.alpha == 0 else "LA" + mode = "L" if not self.alpha else "LA" elif cspace.n == 3: - mode = "RGB" if self.alpha == 0 else "RGBA" + mode = "RGB" if not self.alpha else "RGBA" else: mode = "CMYK" img = Image.frombytes(mode, (self.width, self.height), self.samples) + return img + + def pil_save(self, *args, **kwargs): + """Write to image file using Pillow. + + An intermediate PIL Image is created, and its "save" method is used + to store the image. See Pillow documentation to learn about the + meaning of possible positional and keyword parameters. + Use this when other output formats are desired. + """ + img = self.pil_image() if "dpi" not in kwargs.keys(): kwargs["dpi"] = (self.xres, self.yres) @@ -10257,14 +10267,20 @@ def pil_save(self, *args, **kwargs): img.save(*args, **kwargs) def pil_tobytes(self, *args, **kwargs): - """Convert to binary image stream using pillow. + """Convert to an image in memory using Pillow. - Args are passed to Pillow's Image.save method, see their documentation. - Use instead of 'tobytes' when other output formats are needed. + An intermediate PIL Image is created, and its "save" method is used + to store the image. See Pillow documentation to learn about the + meaning of possible positional or keyword parameters. + Use this when other output formats are desired. """ - from io import BytesIO - bytes_out = BytesIO() - self.pil_save(bytes_out, *args, **kwargs) + bytes_out = io.BytesIO() + img = self.pil_image() + + if "dpi" not in kwargs.keys(): + kwargs["dpi"] = (self.xres, self.yres) + + img.save(bytes_out, *args, **kwargs) return bytes_out.getvalue() def pixel(self, x, y):