Skip to content

Commit

Permalink
Address issue 4225
Browse files Browse the repository at this point in the history
We now cover an additional case of missing / not provided color space, as suggested in the issue description.
We are also taking the opportunity to provide a method that directly creates a PIL Image.
  • Loading branch information
JorjMcKie committed Jan 14, 2025
1 parent 00f2309 commit 611a4f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
14 changes: 10 additions & 4 deletions docs/pixmap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
43 changes: 28 additions & 15 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10227,44 +10227,57 @@ 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:
message("PIL/Pillow not installed")
raise

cspace = self.colorspace
if cspace is None:
if not cspace or cspace.name == "None":
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)

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):
Expand Down

0 comments on commit 611a4f9

Please sign in to comment.