Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 32 additions & 16 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 cs.name == "None":
return None
return cs

def copy(self, src, bbox):
"""Copy bbox from another Pixmap."""
Expand Down Expand Up @@ -10227,44 +10230,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:
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
Loading