OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Overlaying two PDFs with an alpha mask at 50%

  • Thread starter Thread starter Hadriensz
  • Start date Start date
H

Hadriensz

Guest
I'm trying to recovers notes I took on an iPad over a PDF, that I saved as a new PDF before the application crashed. This new PDF is corrupted, but I could repair it so that it contains all my notes (highlights and margin scripted notes) but not the original PDF.

I am trying to use the fitz library (a.k.a PyMuPDF) to recover the full notes by overlaying the original PDF with my notes (using an alpha mask of 50% so that I can see through my highlights).

Unfortunately I could not manage to overlay two pages with transparency! The notes page is always masking the original PDF, so that I only see highlights and scripted notes on a BLANK page.

Example of 1 page:

original PDF (page 276


  • 1 page of notes I want to overlay:

notes (page 276


I have tried the following code and a few variants without success (note in the following code I'm only trying to create one page -- page 276 -- of the whole document to speed up the test):

Code:
import fitz  # PyMuPDF 
journal_document = fitz.open(journal_path) # type: ignore 
notes_document = fitz.open(notes_path) # type: ignore 
combined_document = fitz.open() # type: ignore

for page_num in range(len(journal_document)):
    
    if page_num<276:
        continue
    
    # load pages to overlay
    journal_page = journal_document.load_page(page_num)
    notes_page = notes_document.load_page(page_num) 
    
    # extract bottom image 
    journal_pix = journal_page.get_pixmap()
    journal_image = fitz.Pixmap(journal_pix, 0)
    
    # create a new page in output doc
    combined_page = combined_document.new_page(width=journal_page.rect.width,
    height=journal_page.rect.height)
    combined_page.show_pdf_page(journal_page.rect, journal_document, page_num)
    
    # extract notes to be overlayed 
    notes_pix = notes_page.get_displaylist().get_pixmap()
    notes_image = fitz.Pixmap(notes_pix)
    notes_image.set_alpha(bytearray(int(128)) * 595 * 842)
    
    # Insérer l'image du journal sur la nouvelle page
    combined_page.insert_image(notes_page.rect, stream=notes_image.tobytes(), 
    alpha = int(128))
    
    print(f"page {page_num} saved...")
    
    break

combined_document.save(output_path)
<p>I'm trying to recovers notes I took on an iPad over a PDF, that I saved as a new PDF before the application crashed. This new PDF is corrupted, but I could repair it so that it contains all my notes (highlights and margin scripted notes) but not the original PDF.</p>
<p>I am trying to use the <code>fitz</code> library (a.k.a PyMuPDF) to recover the full notes by overlaying the original PDF with my notes (using an alpha mask of 50% so that I can see through my highlights).</p>
<p>Unfortunately I could not manage to overlay two pages with transparency! The notes page is always masking the original PDF, so that I only see highlights and scripted notes on a BLANK page.</p>
<p>Example of 1 page:</p>
<p><img src="https://i.sstatic.net/e7neFGvI.jpg" alt="original PDF (page 276" /></p>
<ul>
<li>1 page of notes I want to overlay:</li>
</ul>
<p><img src="https://i.sstatic.net/YjYXzfnx.png" alt="notes (page 276" /></p>
<p>I have tried the following code and a few variants without success (note in the following code I'm only trying to create one page -- page 276 -- of the whole document to speed up the test):</p>
<pre><code>import fitz # PyMuPDF
journal_document = fitz.open(journal_path) # type: ignore
notes_document = fitz.open(notes_path) # type: ignore
combined_document = fitz.open() # type: ignore

for page_num in range(len(journal_document)):

if page_num<276:
continue

# load pages to overlay
journal_page = journal_document.load_page(page_num)
notes_page = notes_document.load_page(page_num)

# extract bottom image
journal_pix = journal_page.get_pixmap()
journal_image = fitz.Pixmap(journal_pix, 0)

# create a new page in output doc
combined_page = combined_document.new_page(width=journal_page.rect.width,
height=journal_page.rect.height)
combined_page.show_pdf_page(journal_page.rect, journal_document, page_num)

# extract notes to be overlayed
notes_pix = notes_page.get_displaylist().get_pixmap()
notes_image = fitz.Pixmap(notes_pix)
notes_image.set_alpha(bytearray(int(128)) * 595 * 842)

# Insérer l'image du journal sur la nouvelle page
combined_page.insert_image(notes_page.rect, stream=notes_image.tobytes(),
alpha = int(128))

print(f"page {page_num} saved...")

break

combined_document.save(output_path)
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top