Skip to content

Commit

Permalink
Changed has_transparency_data() to property
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 26, 2023
1 parent 1c30809 commit e27d7a6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,27 +909,27 @@ def test_zero_tobytes(self, size):
def test_has_transparency_data(self):
for mode in ("1", "L", "P", "RGB"):
im = Image.new(mode, (1, 1))
assert not im.has_transparency_data()
assert not im.has_transparency_data

for mode in ("LA", "La", "PA", "RGBA", "RGBa"):
im = Image.new(mode, (1, 1))
assert im.has_transparency_data()
assert im.has_transparency_data

# P mode with "transparency" info
with Image.open("Tests/images/first_frame_transparency.gif") as im:
assert "transparency" in im.info
assert im.has_transparency_data()
assert im.has_transparency_data

# RGB mode with "transparency" info
with Image.open("Tests/images/rgb_trns.png") as im:
assert "transparency" in im.info
assert im.has_transparency_data()
assert im.has_transparency_data

# P mode with RGBA palette
im = Image.new("RGBA", (1, 1)).convert("P")
assert im.mode == "P"
assert im.palette.mode == "RGBA"
assert im.has_transparency_data()
assert im.has_transparency_data

def test_apply_transparency(self):
im = Image.new("P", (1, 1))
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/Image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ This helps to get the bounding box coordinates of the input image::
.. automethod:: PIL.Image.Image.getpalette
.. automethod:: PIL.Image.Image.getpixel
.. automethod:: PIL.Image.Image.getprojection
.. automethod:: PIL.Image.Image.has_transparency_data
.. autoproperty:: PIL.Image.Image.has_transparency_data
.. automethod:: PIL.Image.Image.histogram
.. automethod:: PIL.Image.Image.paste
.. automethod:: PIL.Image.Image.point
Expand Down
1 change: 1 addition & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,7 @@ def getpalette(self, rawmode="RGB"):
rawmode = mode
return list(self.im.getpalette(mode, rawmode))

@property
def has_transparency_data(self) -> bool:
"""
Determine if an image has transparency data, whether in the form of an
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def _save(im, fp, filename):
exact = 1 if im.encoderinfo.get("exact") else 0

if im.mode not in _VALID_WEBP_LEGACY_MODES:
im = im.convert("RGBA" if im.has_transparency_data() else "RGB")
im = im.convert("RGBA" if im.has_transparency_data else "RGB")

data = _webp.WebPEncode(
im.tobytes(),
Expand Down

0 comments on commit e27d7a6

Please sign in to comment.