Translate the following sentence: “The coordinates obtained from pdfminer are corrected on the PIL image.”

  Kiến thức lập trình

I obtained some coordinate information (such as the coordinates of rectangles, etc.) using pdfminer. Now I want to convert the PDF pages into images and then draw the coordinates obtained from pdfminer onto the images.

this is my code:

    def draw_image(self):
        json_path = os.path.dirname(self.store_path.rstrip('/'))+ '/coordinates_'+ self.file_name+ '.json'
        pdf_document = fitz.open(self.pdf_path)
        color_dict = {'rect': ['green', 1], 'figure': ['red', 1], 'line': ['blue', 1]}
        with open(json_path, 'r', encoding= 'utf-8') as json_file:
            table_dict = json.load(json_file)
            tqdm_image = tqdm(table_dict.items(), total= len(self.page_num))
            for page_number, info in tqdm_image:
                page = pdf_document.load_page(int(page_number))
                pix = page.get_pixmap(dpi=192)
                # pix = page.get_pixmap(matrix=fitz.Matrix(1, 1), dpi=page.rect.width / page.rect.height * 100)
                image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
                image_draw = ImageDraw.Draw(image)
                for layout, coords in info.items():
                    if len(coords):
                        for coord in coords:
                            coordinates = [((pix.width- point[0], pix.height - point[1])) for point in coord]
                            image_draw.line(coordinates, color_dict[layout][0], width= color_dict[layout][1])
                    # for point in coords:
                    #     image_draw.ellipse(point[0][0]-2, point[0][1]-2, point[0][0])
                tqdm_image.set_postfix(now_page = page_number, total = len(self.page_num))
                image.save(f'{self.store_path}{self.file_name}-{page_number}.png')
        pdf_document.close()

LEAVE A COMMENT