ModularM
Modular15mo ago
1 reply
Hasan Yousef

Python integration: address not mapped to object

I wrote the below simple python code at jupyter notebook for stamping my pdf documents:
import fitz  # PyMuPDF
def add_signature(input_pdf, output_pdf, signature_image, x, y, stamp_width, stamp_height):
    doc = fitz.open(input_pdf)

    # Iterate through each page and insert the signature
    for page in doc:
        rect = fitz.Rect(x, y, x + stamp_width, y + stamp_height)  # Use defined width and height
        page.insert_image(rect, filename=signature_image)

    doc.save(output_pdf)
    doc.close()

    print(f"Signature added to {output_pdf}.")


I tried writing a mojo code for the same, so wrote:

from python import Python

def add_signature(input_pdf: String, output_pdf: String, signature_image: String, x: Int, y: Int, stamp_width: Int, stamp_height: Int):

    var fitz = Python.import_module("fitz")
    var doc = fitz.open(input_pdf)

    for page in doc:
        rect = fitz.Rect(x, y, x + stamp_width, y + stamp_height)  # Define rectangle for the signature
        page.insert_image(rect, filename=signature_image)  # Insert the image

    doc.save(output_pdf)
    doc.close()

    print("Signature added to ", output_pdf)


I'm calling the function, in bot python and mojo using:

input_pdf = "input.pdf"
output_pdf = "signed_output.pdf"
signature_image = "my_stamp.png"
x_position = 300  # X position for signature
y_position = 600   # Y position for signature
stamp_width = 200  # Set desired width
stamp_height = 100  # Set desired height

add_signature(input_pdf, output_pdf, signature_image, x_position, y_position, stamp_width, stamp_height)


With python I got the output.dpf correctly, but with mojo I got the error:

error: Execution was interrupted, reason: signal SIGSEGV: address not mapped to object (fault address: 0x4d).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.


What could be my mistake here?
Was this page helpful?