用python捕获部分pdf文件

用python捕获部分pdf文件,python,image,Python,Image,我有两页的pdf文件,在第一页的顶部有一个条形码 有没有办法只从python中扫描的pdf文件中捕获条形码?我已经搜索过了,但没有找到这样的问题。你能帮助我,甚至任何一个链接作为线索,我会研究的链接,并试图实现它自己 我找到了这样的代码,但无法修改以仅裁剪第一页的顶部 from PyPDF2 import PdfFileWriter, PdfFileReader with open("Sample.pdf", "rb") as in_f: in

我有两页的pdf文件,在第一页的顶部有一个条形码

有没有办法只从python中扫描的pdf文件中捕获条形码?我已经搜索过了,但没有找到这样的问题。你能帮助我,甚至任何一个链接作为线索,我会研究的链接,并试图实现它自己

我找到了这样的代码,但无法修改以仅裁剪第一页的顶部

from PyPDF2 import PdfFileWriter, PdfFileReader

with open("Sample.pdf", "rb") as in_f:
    input1 = PdfFileReader(in_f)
    output = PdfFileWriter()
    
    numPages = input1.getNumPages()
    print("Document Has %s pages." % numPages)
    
    for i in range(numPages):
        page = input1.getPage(i)
        print(page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y())
        page.trimBox.lowerLeft = (25, 25)
        page.trimBox.upperRight = (225, 225)
        page.cropBox.lowerLeft = (50, 50)
        page.cropBox.upperRight = (200, 200)
        output.addPage(page)

    with open("Output.pdf", "wb") as out_f:
        output.write(out_f)

为了检测和提取条形码,最简单的方法是将PDF转换为图像,然后在图像上运行条形码检测。可以使用进行图像转换。检测条形码的一种标准方法是使用zbar,例如,用于,但我没有得到图像的检测结果,可能是因为扫描的质量。或者,可以使用Opencv提取条形码。对于下面的工作示例,我将答案中的代码重新用于post:

输出:

非常感谢。PDF文件仅为扫描图像,因此PDF内容仅为图像,条形码部分仅为PDF内部图像的一部分。我不确定这是否有帮助。我只需要修剪那部分。我认为在主要职位的代码是好的,但我不知道如何调整数字,以适应我的情况下,我需要的条码部分与下面的数字。。我感兴趣的是数字,而不是条形码。你需要从pdf中检索图像,以便进行条形码识别。根据您的示例,这可以通过PyPDF2实现,但事实并非如此。您可以在图像输出上运行OCR以获得条形码编号。pdf2image包依赖poppler utils:
sudo apt get install poppler utils
。您可以在max_y和max_x中添加
+30
,在
框[…]之后添加
以扩大裁剪范围,使其包含编号。来自另一个页面的点击是误报,因此您也可以只处理
图像[0]
,而不是两个页面。
from pdf2image import convert_from_path, convert_from_bytes
import numpy as np
import imutils
import cv2

from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)

images = convert_from_path('Sample.pdf')

#convert PIL images to cv2
images = [np.array(i)[:, :, ::-1] for i in images]

for nr, image in enumerate(images):
  # convert it to grayscale
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

  # compute the Scharr gradient magnitude representation of the images
  # in both the x and y direction using OpenCV 2.4
  ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
  gradX = cv2.Sobel(gray, ddepth = ddepth, dx = 1, dy = 0, ksize = -1)
  gradY = cv2.Sobel(gray, ddepth = ddepth, dx = 0, dy = 1, ksize = -1)

  # subtract the y-gradient from the x-gradient
  gradient = cv2.subtract(gradX, gradY)
  gradient = cv2.convertScaleAbs(gradient)

  # blur and threshold the image
  blurred = cv2.blur(gradient, (9, 9))
  (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)

  # construct a closing kernel and apply it to the thresholded image
  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
  closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

  # perform a series of erosions and dilations
  closed = cv2.erode(closed, None, iterations = 4)
  closed = cv2.dilate(closed, None, iterations = 4)

  # find the contours in the thresholded image, then sort the contours
  # by their area, keeping only the largest one
  cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = imutils.grab_contours(cnts)
  c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]

  # compute the rotated bounding box of the largest contour
  rect = cv2.minAreaRect(c)
  box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
  box = np.int0(box)

  # draw a bounding box arounded the detected barcode and display the
  min_y = int(np.min(box[:,-1]))
  max_y = int(np.max(box[:,-1]))
  min_x = int(np.min(box[:,0]))
  max_x = int(np.max(box[:,0]))
  image = image[min_y:max_y, min_x:max_x]
  # save cropped image
  cv2.imwrite(f"cropped_{nr}.jpg", image)