Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError: float division by zero when use apply_redactions() #2943

Closed
helloburke opened this issue Dec 26, 2023 · 6 comments
Closed
Labels

Comments

@helloburke
Copy link

helloburke commented Dec 26, 2023

Description of the bug

Traceback (most recent call last):
  File "D:\project\mytest.py", line 14, in <module>
    page.apply_redactions()
  File "D:\project\venv\lib\site-packages\fitz\utils.py", line 4261, in apply_redactions
    trect = center_rect(annot_rect, text, fname, fsize)
  File "D:\project\venv\lib\site-packages\fitz\utils.py", line 4220, in center_rect
    h = math.ceil(text_width / limit) * line_height  # estimate rect height
ZeroDivisionError: float division by zero

How to reproduce the bug

first put the file Equity Compensation Master.pdf Equity Compensation Master.pdf

to data/input/Equity Compensation Master.pdf
then run the code:

import fitz
import json

doc = fitz.open(filename="data/input/Equity Compensation Master.pdf")

page = doc[0]
info = json.loads(page.get_text("json"))
span = info.get("blocks")[3].get("lines")[0].get("spans")[0]

span_text = span.get("text","")
print("span: bbox=",span["bbox"]," text=",span_text)
page.add_redact_annot(span["bbox"], text="New Text", fontsize=span["size"], fill=(1, 1, 0))
page.apply_redactions()

PyMuPDF version

Built from source

Operating system

Windows

Python version

3.10

@JorjMcKie
Copy link
Collaborator

Cannot reproduce:

>py -3.10 test.py


PyMuPDF 1.23.8: Python bindings for the MuPDF 1.23.7 library.
Version date: 2023-12-19 00:00:01.
Built for Python 3.10 on win32 (64-bit).

span: bbox= [272.4049987792969, 309.27484130859375, 447.60748291015625, 343.45452880859375]  text= Frank Demmler

BTW: any particular reason why you would use the JSON output instead of the original "dict"?

@JorjMcKie
Copy link
Collaborator

Further investigation revealed that there is an error in the rebased implementation only, which in turn only occurs if the rectangle in page.add_redact_annot(rect, ...) is not a fitz.Rect but only a rect-like (4-float sequence).
So in your case, you can avoid this by always using page.add_redact_annot(fitz.Rect(span["bbox"]), ...).

@JorjMcKie JorjMcKie added bug fix developed release schedule to be determined labels Dec 26, 2023
@helloburke
Copy link
Author

Further investigation revealed that there is an error in the rebased implementation only, which in turn only occurs if the rectangle in page.add_redact_annot(rect, ...) is not a fitz.Rect but only a rect-like (4-float sequence). So in your case, you can avoid this by always using page.add_redact_annot(fitz.Rect(span["bbox"]), ...).

it works when change code to page.add_redact_annot(fitz.Rect(span["bbox"]), ...).

@JorjMcKie
Copy link
Collaborator

Thanks for your feedback and I am glad it works for you!
Our internal rules however require that keep an issue open until the fixed version is available on PyPI.
So I am going to reopen this.

@JorjMcKie JorjMcKie reopened this Dec 27, 2023
@JorjMcKie JorjMcKie added Fixed in next release and removed fix developed release schedule to be determined labels Dec 27, 2023
@julian-smith-artifex-com
Copy link
Collaborator

This is fixed in release candidate 1.23.9rc1; see: #2960

@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants