mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 01:16:16 +03:00
Added "transparency" argument to load()
This commit is contained in:
parent
426b730d3b
commit
1d73a483f4
BIN
Tests/images/reqd_showpage_transparency.png
Normal file
BIN
Tests/images/reqd_showpage_transparency.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
|
@ -96,6 +96,17 @@ def test_showpage():
|
||||||
assert_image_similar(plot_image, target, 6)
|
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")
|
@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
|
||||||
def test_file_object(tmp_path):
|
def test_file_object(tmp_path):
|
||||||
# issue 479
|
# issue 479
|
||||||
|
|
|
@ -66,7 +66,7 @@ than leaving them in the original color space. The EPS driver can write images
|
||||||
in ``L``, ``RGB`` and ``CMYK`` modes.
|
in ``L``, ``RGB`` and ``CMYK`` modes.
|
||||||
|
|
||||||
If Ghostscript is available, you can call the :py:meth:`~PIL.Image.Image.load`
|
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**
|
**scale**
|
||||||
Affects the scale of the resultant rasterized image. If the EPS suggests
|
Affects the scale of the resultant rasterized image. If the EPS suggests
|
||||||
|
@ -79,6 +79,11 @@ method with the following parameter to affect how Ghostscript renders the EPS
|
||||||
im.load(scale=2)
|
im.load(scale=2)
|
||||||
im.size #(200,200)
|
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
|
GIF
|
||||||
^^^
|
^^^
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ def has_ghostscript():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def Ghostscript(tile, size, fp, scale=1):
|
def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
||||||
"""Render an image using Ghostscript"""
|
"""Render an image using Ghostscript"""
|
||||||
|
|
||||||
# Unpack decoder tile
|
# Unpack decoder tile
|
||||||
|
@ -108,6 +108,8 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
lengthfile -= len(s)
|
lengthfile -= len(s)
|
||||||
f.write(s)
|
f.write(s)
|
||||||
|
|
||||||
|
device = "pngalpha" if transparency else "ppmraw"
|
||||||
|
|
||||||
# Build Ghostscript command
|
# Build Ghostscript command
|
||||||
command = [
|
command = [
|
||||||
"gs",
|
"gs",
|
||||||
|
@ -117,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1):
|
||||||
"-dBATCH", # exit after processing
|
"-dBATCH", # exit after processing
|
||||||
"-dNOPAUSE", # don't pause between pages
|
"-dNOPAUSE", # don't pause between pages
|
||||||
"-dSAFER", # safe mode
|
"-dSAFER", # safe mode
|
||||||
"-sDEVICE=ppmraw", # ppm driver
|
f"-sDEVICE={device}",
|
||||||
f"-sOutputFile={outfile}", # output file
|
f"-sOutputFile={outfile}", # output file
|
||||||
# adjust for image origin
|
# adjust for image origin
|
||||||
"-c",
|
"-c",
|
||||||
|
@ -325,11 +327,11 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
return (length, offset)
|
return (length, offset)
|
||||||
|
|
||||||
def load(self, scale=1):
|
def load(self, scale=1, transparency=False):
|
||||||
# Load EPS via Ghostscript
|
# Load EPS via Ghostscript
|
||||||
if not self.tile:
|
if not self.tile:
|
||||||
return
|
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.mode = self.im.mode
|
||||||
self._size = self.im.size
|
self._size = self.im.size
|
||||||
self.tile = []
|
self.tile = []
|
||||||
|
|
Loading…
Reference in New Issue
Block a user