【问题标题】:How to add background image in pdf using Pymupdf module in python如何在 python 中使用 Pymupdf 模块在 pdf 中添加背景图像
【发布时间】:2021-12-29 04:00:14
【问题描述】:

我正在尝试使用 Pymupdf 在 pdf 中添加背景图像,但它在 pdf 和图像之间创建了一个图层,您可以看到输出。

如何绕过(删除)pdf 和背景图像之间的层?请帮我重新评分。

这就是我在 pdf 中添加背景图像的方式:

import fitz
pdf_name = '3_giberish template.pdf'[enter image description here][1]
doc = fitz.open(pdf_name)
doc = fitz.open(input_file)
#open page first
page = doc.loadPage(0)
background_img_filename = 'background.png'
# insert background image to the full page
full_img_rect = fitz.Rect(0,0,650,792)
#overlay = False add the background image here
page.insertImage(full_img_rect, filename=background_img_filename, overlay=False)
# save doc
doc.save(output_file_path, garbage=4, deflate=True, clean=True)
print("completed")

【问题讨论】:

  • 请从这里获取输出链接:imgur.com/a/9uryHYP
  • 你的意思是你只希望文本在'background.png'而不是白色背景?
  • 嗨@manaclan 感谢您的回复,您是对的,这正是我想要的。

标签: python pdf pymupdf


【解决方案1】:

您好,这个答案可能不是最佳答案,但我认为它会对您有所帮助。
首先,您需要将 pdf 页面转换为 RGBA 图像。然后,每个白色像素将被转换为透明,以便在叠加到背景上时使其消失。
之后,如果背景小于文本图像,则将其放大以适合文本。
最后把文字图片放在背景上面,导出为pdf。
下面是我测试的图像。
text.jpg

background.jpg res.png

"""
pip install opencv-python
pip install pymupdf
pip install Pillow
"""

import fitz
import cv2
import numpy as np
from PIL import Image


def pix2np(pix):
    im = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.h, pix.w, pix.n)
    im = np.ascontiguousarray(im[..., [2, 1, 0]])  # rgb to bgr
    return im

def resize(img,scale_percent):
  width = int(img.shape[1] * scale_percent / 100)
  height = int(img.shape[0] * scale_percent / 100)
  dim = (width, height)
    
  # resize image
  return cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

doc = fitz.open('text.pdf')
# fitz to opencv image
# https://study.marearts.com/2020/04/pdf-to-opencv-as-page-by-page-using.html
for page_num, page in enumerate(doc.pages()):
  mat = fitz.Matrix(1, 1)
  pix = page.get_pixmap(matrix = mat)
  im = pix2np(pix)

  # white border removed and keep the text
  # https://stackoverflow.com/a/49907762/7828101
  gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
  gray = 255*(gray < 128).astype(np.uint8) # invert the text to white
  coords = cv2.findNonZero(gray) # Find all non-zero points (text)
  x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
  rect = im[y:y+h, x:x+w] # Crop the image - note we do this on the original image
  # cv2.imwrite('res.jpg',rect)

  # convert white background to transparent background
  new_img = cv2.cvtColor(rect, cv2.COLOR_BGR2BGRA)
  for i in range(new_img.shape[0]):
    for j in range(new_img.shape[1]):
      pixel = new_img[i,j]
      for k,value in enumerate(pixel):
        if value <250:
          break
        if k == 3:
          new_img[i,j,3] = 0

  # paste result image to background
  background = cv2.imread('background.jpg')
  background = cv2.cvtColor(background, cv2.COLOR_BGR2BGRA)
  if background.shape[0] < new_img.shape[0]:
    scale_percent = new_img.shape[0]/background.shape[0]
    background = resize(background,scale_percent)
  if background.shape[1] < new_img.shape[1]:
    scale_percent = new_img.shape[1]/background.shape[1]
    background = resize(background,scale_percent)
  
  y_position = int((background.shape[0] - new_img.shape[0])/2)
  x_position = int((background.shape[1] - new_img.shape[1])/2)

  # Merge two image
  # https://stackoverflow.com/a/14102014/7828101
  alpha_s = new_img[:, :, 3] / 255.0
  alpha_l = 1.0 - alpha_s

  for i in range(0,3):
    new_img_inside_background = background[y_position:y_position + new_img.shape[0],
                x_position:x_position + new_img.shape[1],:]
    background[y_position:y_position + new_img.shape[0],
                x_position:x_position + new_img.shape[1],i] = (alpha_s * new_img[:, :, i] +
                              alpha_l * new_img_inside_background[:,:,i])

  cv2.imwrite('res.png',background)
  background = cv2.cvtColor(background, cv2.COLOR_BGRA2RGB)
  im_pil = Image.fromarray(background)
  im_pil.save('{}_res.pdf'.format(page_num))

【讨论】:

    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多