mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-22 05:53:22 +03:00
merge() function tool and coverage improvement
This commit is contained in:
parent
99dd55324d
commit
812925ed49
11
.coveragerc
11
.coveragerc
|
@ -18,7 +18,10 @@ exclude_also =
|
|||
|
||||
[run]
|
||||
omit =
|
||||
Tests/32bit_segfault_check.py
|
||||
Tests/bench_cffi_access.py
|
||||
Tests/check_*.py
|
||||
Tests/createfontdatachunk.py
|
||||
# Tests/32bit_segfault_check.py
|
||||
# Tests/bench_cffi_access.py
|
||||
# Tests/check_*.py
|
||||
# Tests/createfontdatachunk.py
|
||||
Tests/*
|
||||
src/*
|
||||
|
||||
|
|
|
@ -1087,3 +1087,6 @@ class TestRegistry:
|
|||
def test_encode_registry_fail(self) -> None:
|
||||
with pytest.raises(OSError):
|
||||
Image._getencoder("RGB", "DoesNotExist", ("args",), extra=("extra",))
|
||||
|
||||
|
||||
|
||||
|
|
25
Tests/test_image_merge.py
Normal file
25
Tests/test_image_merge.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
import pytest
|
||||
|
||||
from PIL import Image
|
||||
|
||||
def test_merge_wrong_number_of_bands():
|
||||
R = Image.new('L', (100, 100), color=255)
|
||||
G = Image.new('L', (100, 100), color=128)
|
||||
with pytest.raises(ValueError, match="wrong number of bands"):
|
||||
Image.merge('RGB', [R, G])
|
||||
|
||||
def test_merge_mode_mismatch():
|
||||
R = Image.new('L', (100, 100), color=255)
|
||||
G = Image.new('L', (100, 100), color=128)
|
||||
B = Image.new('1', (100, 100)) # Incorrect mode
|
||||
with pytest.raises(ValueError, match="mode mismatch"):
|
||||
Image.merge('RGB', [R, G, B])
|
||||
|
||||
def test_merge_size_mismatch():
|
||||
R = Image.new('L', (100, 100), color=255)
|
||||
G = Image.new('L', (200, 100), color=128) # Different size
|
||||
B = Image.new('L', (100, 100), color=0)
|
||||
with pytest.raises(ValueError, match="size mismatch"):
|
||||
Image.merge('RGB', [R, G, B])
|
||||
|
||||
|
22
coveragetool.py
Normal file
22
coveragetool.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from PIL import Image
|
||||
from PIL.Image import branches
|
||||
|
||||
# Define a function to calculate and print branch coverage
|
||||
|
||||
|
||||
def calculate_branch_coverage():
|
||||
num_branches = len(branches)
|
||||
branch_covered = {key: value for key, value in branches.items() if value is True}
|
||||
sum_branches = len(branch_covered)
|
||||
coverage = (sum_branches/num_branches) * 100
|
||||
print(f"Branches covered: {sum_branches}")
|
||||
print(f"Total branches: {num_branches}")
|
||||
print("\nBRANCH COVERAGE:", coverage, "%\n")
|
||||
|
||||
R = Image.new('L', (100, 100), color=255)
|
||||
G = Image.new('L', (100, 100), color=128)
|
||||
B = Image.new('L', (100, 100), color=0)
|
||||
merged_image = Image.merge('RGB', (R, G, B))
|
||||
merged_image.save('merged_image.png')
|
||||
|
||||
calculate_branch_coverage()
|
BIN
merged_image.png
Normal file
BIN
merged_image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 289 B |
|
@ -75,6 +75,14 @@ class DecompressionBombWarning(RuntimeWarning):
|
|||
class DecompressionBombError(Exception):
|
||||
pass
|
||||
|
||||
branches = {
|
||||
"1": False,
|
||||
"2": False,
|
||||
"3": False,
|
||||
"4": False,
|
||||
"5": False,
|
||||
}
|
||||
|
||||
|
||||
# Limit to around a quarter gigabyte for a 24-bit (3 bpp) image
|
||||
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
|
||||
|
@ -3521,16 +3529,21 @@ def merge(mode: str, bands: Sequence[Image]) -> Image:
|
|||
"""
|
||||
|
||||
if getmodebands(mode) != len(bands) or "*" in mode:
|
||||
branches["1"] = True
|
||||
msg = "wrong number of bands"
|
||||
raise ValueError(msg)
|
||||
for band in bands[1:]:
|
||||
branches["2"] = True
|
||||
if band.mode != getmodetype(mode):
|
||||
branches["3"] = True
|
||||
msg = "mode mismatch"
|
||||
raise ValueError(msg)
|
||||
if band.size != bands[0].size:
|
||||
branches["4"] = True
|
||||
msg = "size mismatch"
|
||||
raise ValueError(msg)
|
||||
for band in bands:
|
||||
branches["5"] = True
|
||||
band.load()
|
||||
return bands[0]._new(core.merge(mode, *[b.im for b in bands]))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user