Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "transparency" argument to EpsImagePlugin load() #5620

Merged
merged 4 commits into from Aug 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/reqd_showpage_transparency.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Tests/test_file_eps.py
Expand Up @@ -96,6 +96,17 @@ def test_showpage():
assert_image_similar(plot_image, target, 6)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_transparency():
with Image.open("Tests/images/reqd_showpage.eps") as plot_image:
plot_image.load(transparency=True)
assert plot_image.mode == "RGBA"

with Image.open("Tests/images/reqd_showpage_transparency.png") as target:
# fonts could be slightly different
assert_image_similar(plot_image, target, 6)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_file_object(tmp_path):
# issue 479
Expand Down
7 changes: 6 additions & 1 deletion docs/handbook/image-file-formats.rst
Expand Up @@ -66,7 +66,7 @@ than leaving them in the original color space. The EPS driver can write images
in ``L``, ``RGB`` and ``CMYK`` modes.

If Ghostscript is available, you can call the :py:meth:`~PIL.Image.Image.load`
method with the following parameter to affect how Ghostscript renders the EPS
method with the following parameters to affect how Ghostscript renders the EPS

**scale**
Affects the scale of the resultant rasterized image. If the EPS suggests
Expand All @@ -79,6 +79,11 @@ method with the following parameter to affect how Ghostscript renders the EPS
im.load(scale=2)
im.size #(200,200)

**transparency**
If true, generates an RGBA image with a transparent background, instead of
the default behaviour of an RGB image with a white background.


GIF
^^^

Expand Down
12 changes: 12 additions & 0 deletions docs/releasenotes/8.4.0.rst
Expand Up @@ -19,6 +19,18 @@ the default required length, also removing the need for the size parameter.
API Additions
=============

Added "transparency" argument for loading EPS images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This new argument switches the Ghostscript device from "ppmraw" to "pngalpha",
generating an RGBA image with a transparent background instead of an RGB image with a
white background.

.. code-block:: python

with Image.open("sample.eps") as im:
im.load(transparency=True)

Added WalImageFile class
^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
10 changes: 6 additions & 4 deletions src/PIL/EpsImagePlugin.py
Expand Up @@ -61,7 +61,7 @@ def has_ghostscript():
return False


def Ghostscript(tile, size, fp, scale=1):
def Ghostscript(tile, size, fp, scale=1, transparency=False):
"""Render an image using Ghostscript"""

# Unpack decoder tile
Expand Down Expand Up @@ -108,6 +108,8 @@ def Ghostscript(tile, size, fp, scale=1):
lengthfile -= len(s)
f.write(s)

device = "pngalpha" if transparency else "ppmraw"

# Build Ghostscript command
command = [
"gs",
Expand All @@ -117,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1):
"-dBATCH", # exit after processing
"-dNOPAUSE", # don't pause between pages
"-dSAFER", # safe mode
"-sDEVICE=ppmraw", # ppm driver
f"-sDEVICE={device}",
f"-sOutputFile={outfile}", # output file
# adjust for image origin
"-c",
Expand Down Expand Up @@ -325,11 +327,11 @@ def _find_offset(self, fp):

return (length, offset)

def load(self, scale=1):
def load(self, scale=1, transparency=False):
# Load EPS via Ghostscript
if not self.tile:
return
self.im = Ghostscript(self.tile, self.size, self.fp, scale)
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
self.mode = self.im.mode
self._size = self.im.size
self.tile = []
Expand Down