Skip to content

Commit

Permalink
Address 3848
Browse files Browse the repository at this point in the history
  • Loading branch information
JorjMcKie committed Oct 6, 2024
1 parent b68f183 commit a526e3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10021,11 +10021,14 @@ def color_count(self, colors=0, clip=None):

def color_topusage(self, clip=None):
"""Return most frequent color and its usage ratio."""
if clip is not None:
clip = IRect(self.irect) & clip
if clip.is_empty:
raise ValueError("clip must not be empty")
allpixels = 0
cnt = 0
if clip is not None and self.irect in Rect(clip):
clip = self.irect
for pixel, count in self.color_count(colors=True,clip=clip).items():

for pixel, count in self.color_count(colors=True, clip=clip).items():
allpixels += count
if count > cnt:
cnt = count
Expand Down
3 changes: 3 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ def get_pixmap(
if colorspace.n not in (1, 3, 4):
raise ValueError("unsupported colorspace")

if clip is not None and pymupdf.IRect(clip).is_empty:
raise ValueError("clip must not be empty")

dl = page.get_displaylist(annots=annots)
pix = dl.get_pixmap(matrix=matrix, colorspace=colorspace, alpha=alpha, clip=clip)
dl = None
Expand Down
10 changes: 10 additions & 0 deletions tests/test_pixmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ def test_3177():
pixmap = pymupdf.Pixmap(path)
pixmap2 = pymupdf.Pixmap(None, pixmap)

def test_3848():
path = os.path.abspath(f"{__file__}/../../tests/resources/img-transparent.png")
pix = pymupdf.Pixmap(path)
error = False
try:
top = pix.color_topusage(clip=(0, 0, 0, 0))
except ValueError:
error = True
assert error


def test_3493():
'''
Expand Down

0 comments on commit a526e3b

Please sign in to comment.