Skip to content

Commit a526e3b

Browse files
committed
Address 3848
1 parent b68f183 commit a526e3b

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10021,11 +10021,14 @@ def color_count(self, colors=0, clip=None):
1002110021

1002210022
def color_topusage(self, clip=None):
1002310023
"""Return most frequent color and its usage ratio."""
10024+
if clip is not None:
10025+
clip = IRect(self.irect) & clip
10026+
if clip.is_empty:
10027+
raise ValueError("clip must not be empty")
1002410028
allpixels = 0
1002510029
cnt = 0
10026-
if clip is not None and self.irect in Rect(clip):
10027-
clip = self.irect
10028-
for pixel, count in self.color_count(colors=True,clip=clip).items():
10030+
10031+
for pixel, count in self.color_count(colors=True, clip=clip).items():
1002910032
allpixels += count
1003010033
if count > cnt:
1003110034
cnt = count

src/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,9 @@ def get_pixmap(
881881
if colorspace.n not in (1, 3, 4):
882882
raise ValueError("unsupported colorspace")
883883

884+
if clip is not None and pymupdf.IRect(clip).is_empty:
885+
raise ValueError("clip must not be empty")
886+
884887
dl = page.get_displaylist(annots=annots)
885888
pix = dl.get_pixmap(matrix=matrix, colorspace=colorspace, alpha=alpha, clip=clip)
886889
dl = None

tests/test_pixmap.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ def test_3177():
264264
pixmap = pymupdf.Pixmap(path)
265265
pixmap2 = pymupdf.Pixmap(None, pixmap)
266266

267+
def test_3848():
268+
path = os.path.abspath(f"{__file__}/../../tests/resources/img-transparent.png")
269+
pix = pymupdf.Pixmap(path)
270+
error = False
271+
try:
272+
top = pix.color_topusage(clip=(0, 0, 0, 0))
273+
except ValueError:
274+
error = True
275+
assert error
276+
267277

268278
def test_3493():
269279
'''

0 commit comments

Comments
 (0)