pdf转非常高清的图片(pyqt实现的GUI)

基本流程

1.pdf切分图片

2.对图像进行超分辨

3.合并所有图片

代码

import sys
import time
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QFileDialog)
from PyQt5.QtCore import Qt
import os
import fitz
from PIL import Image, ImageEnhance, ImageFilter

class PDFProcessor(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # 设置窗口标题
        self.setWindowTitle('PDF文件变图片')

        # 创建布局
        layout = QVBoxLayout()

        # 创建标签来显示文件选择状态
        self.label = QLabel('尚未选择文件', self)
        self.label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.label)

        # 创建按钮来选择文件
        self.btn = QPushButton('选择 PDF 文件', self)
        self.btn.clicked.connect(self.openFileDialog)
        layout.addWidget(self.btn)

        # 创建操作按钮
        self.processBtn = QPushButton('处理文件', self)
        self.processBtn.setEnabled(False)
        self.processBtn.clicked.connect(self.processPDF)
        layout.addWidget(self.processBtn)

        # 设置主窗口布局
        self.setLayout(layout)

    def openFileDialog(self):
        # 打开文件对话框,过滤出 PDF 文件
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self, "选择 PDF 文件", "", "PDF Files (*.pdf);;All Files (*)",
                                                  options=options)

        if fileName:
            # 更新标签显示文件已选好
            self.label.setText(f"已选择文件: {fileName}")
            self.selectedFile = fileName
            self.processBtn.setEnabled(True)
        else:
            self.label.setText('尚未选择文件')
            self.processBtn.setEnabled(False)

    def processPDF(self):
        # 模拟处理 PDF 文件的操作
        self.processed_file_path = self.pdf_to_images(self.selectedFile)
        self.processed_img_path = self.enhance_image(self.processed_file_path)
        self.output_path = self.merge_images_vertically(self.processed_img_path)
        # 显示处理结果
        self.label.setText(f"文件处理完成: {self.processed_file_path}")

    def pdf_to_images(self, pdf_file, resolution=300):
        # Create output folder if it doesn't exist
        output_folder = os.path.join(os.path.dirname(pdf_file), '!!result!!')

        if not os.path.exists(output_folder):
            os.makedirs(output_folder)
        # Open the PDF file
        doc = fitz.open(pdf_file)
        # Iterate through each page in the PDF
        for page_num in range(len(doc)):
            page = doc.load_page(page_num)
            # Convert page to image (PIL.Image object)
            pixmap = page.get_pixmap(matrix=fitz.Matrix(resolution / 72, resolution / 72))
            img = Image.frombytes("RGB", [pixmap.width, pixmap.height], pixmap.samples)
            # Save the image with high resolution
            image_path = f"{output_folder}/page_{page_num + 1}.png"
            img.save(image_path, dpi=(resolution, resolution))
        doc.close()
        self.label.setText(f"文件第一步处理完成,开始第二步提高图片分辨率")
        time.sleep(2)
        return output_folder

    def enhance_image(self, folder_path, contrast_factor=1.5, sharpness_factor=2.0):
        i = 0
        output_folder = os.path.join(folder_path, 'output_high_resolution')
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)  # 创建目录
        for filename in os.listdir(folder_path):
            # 构造完整的文件路径
            file_path = os.path.join(folder_path, filename)

            # 检查文件是否为图片(可以根据需要扩展支持的格式)
            if os.path.isfile(file_path) and filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
                input_image = file_path
                output_image = f"{output_folder}/{i}.jpg"
                i += 1

                img = Image.open(input_image)
                # Convert RGBA to RGB if the image has an alpha channel
                if img.mode == 'RGBA':
                    img = img.convert('RGB')

                # Enhance contrast
                enhancer = ImageEnhance.Contrast(img)
                img_contrast = enhancer.enhance(contrast_factor)

                # Enhance sharpness
                img_sharp = img_contrast.filter(ImageFilter.UnsharpMask)
                img_sharp = ImageEnhance.Sharpness(img_sharp).enhance(sharpness_factor)

                # Save the enhanced image
                img_sharp.save(output_image)
        self.label.setText(f"文件第二步处理完成,开始第三步合并图片")
        time.sleep(2)
        return output_folder

    def merge_images_vertically(self, folder_path):
        output_path = os.path.join(os.path.dirname(folder_path), 'merged_image.jpg')
        # 获取文件夹中的所有图片文件,并按照顺序排序
        image_files = sorted(
            [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))],
            key=lambda x: int(os.path.splitext(x)[0])  # 按文件名的数字部分排序
        )
        images = [Image.open(os.path.join(folder_path, f)) for f in image_files]  # 打开所有图片
        # 计算合并后图片的总高度和最大宽度
        total_height = sum(img.height for img in images)
        max_width = max(img.width for img in images)

        # 创建一个新的空白图像,用于合并
        merged_image = Image.new('RGB', (max_width, total_height))

        # 将每张图片粘贴到合并后的图像上
        current_height = 0
        for img in images:
            merged_image.paste(img, (0, current_height))
            current_height += img.height
        # 保存合并后的图像
        merged_image.save(output_path)
        time.sleep(2)
        return output_path

# 主程序入口
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = PDFProcessor()
    ex.resize(600, 400)
    ex.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恶心猫charming

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值