开始压缩

简介: 【9月更文挑战第05天】

image.png
更改说明:
临时图像文件:将 Pixmap 对象保存到临时文件中,然后将该图像插入到页面。这可以避免直接处理 Pixmap 对象可能带来的问题。

import os
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import fitz  # PyMuPDF


# 压缩PDF文件
def compress_pdf(input_file, output_file, compression_ratio, progress_callback):
    # 打开 PDF 文件
    doc = fitz.open(input_file)
    num_pages = len(doc)

    # 压缩过程
    for i in range(num_pages):
        page = doc.load_page(i)  # 加载页面

        # 计算新图像尺寸
        scale = compression_ratio / 100.0
        mat = fitz.Matrix(scale, scale)
        pix = page.get_pixmap(matrix=mat)  # 使用缩放比例渲染页面

        # 清除当前页面内容
        page.clean_contents()

        # 使用缩放后的图像插入到页面
        rect = fitz.Rect(0, 0, pix.width, pix.height)
        # 将图像保存到临时文件
        temp_image_path = "temp_image.png"
        pix.save(temp_image_path)

        # 插入临时图像到页面
        page.insert_image(rect, filename=temp_image_path)

        # 删除临时图像文件
        os.remove(temp_image_path)

        progress_callback(i + 1, num_pages)

    # 保存压缩后的文件
    doc.save(output_file, garbage=4, deflate=True)
    doc.close()


# 选择文件
def select_file():
    file_path = filedialog.askopenfilename(
        filetypes=[("PDF files", "*.pdf")])
    file_entry.delete(0, tk.END)
    file_entry.insert(0, file_path)


# 更新进度条
def update_progress(current, total):
    progress_var.set(current / total * 100)
    root.update_idletasks()


# 开始压缩
def start_compression():
    input_file = file_entry.get()
    compression_ratio = ratio_entry.get()

    if not input_file:
        messagebox.showerror("Error", "Please select a PDF file")
        return

    if not compression_ratio.isdigit() or not (0 < int(compression_ratio) <= 100):
        messagebox.showerror("Error", "Please enter a valid compression ratio (1-100)")
        return

    compression_ratio = int(compression_ratio)
    output_file = os.path.join(os.path.dirname(input_file), "compressed.pdf")

    try:
        compress_pdf(input_file, output_file, compression_ratio, update_progress)
        messagebox.showinfo("Success", f"File compressed and saved as {output_file}")
    except Exception as e:
        messagebox.showerror("Error", f"An error occurred: {e}")


# 主窗口
root = tk.Tk()
root.title("PDF Compressor")
root.geometry("400x250")

# 文件选择部分
file_frame = tk.Frame(root)
file_frame.pack(pady=10)
file_label = tk.Label(file_frame, text="Select PDF File:")
file_label.pack(side=tk.LEFT)
file_entry = tk.Entry(file_frame, width=30)
file_entry.pack(side=tk.LEFT, padx=5)
file_button = tk.Button(file_frame, text="Browse", command=select_file)
file_button.pack(side=tk.LEFT)

# 压缩比例输入框
ratio_frame = tk.Frame(root)
ratio_frame.pack(pady=10)
ratio_label = tk.Label(ratio_frame, text="Compression Ratio (%):")
ratio_label.pack(side=tk.LEFT)
ratio_entry = tk.Entry(ratio_frame, width=10)
ratio_entry.pack(side=tk.LEFT, padx=5)

# 进度条
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100)
progress_bar.pack(pady=20, fill=tk.X, padx=20)

# 开始压缩按钮
start_button = tk.Button(root, text="Start Compression", command=start_compression)
start_button.pack(pady=10)

# 运行主循环
root.mainloop()
目录
相关文章
|
Web App开发 iOS开发
无法安装此app,因为无法验证其完整性 ,解决方案
无法安装此app,因为无法验证其完整性 ,解决方案
Visual Studio 2022 中VLD库如何安装
Visual Studio 2022 中VLD库如何安装
858 0
|
存储 芯片 iOS开发
苹果M1芯片上运行Stable Diffusion,生成图片只需15秒,几步搞定
苹果M1芯片上运行Stable Diffusion,生成图片只需15秒,几步搞定
1920 0
|
传感器 机器人
数字孪生
数字孪生(Digital Twin)是一种数字模型,它可以模拟现实世界中的物体、设施、系统等的结构、行为和性能。数字孪生技术将物理世界与数字世界相结合,通过实时数据和历史数据,
567 2
|
C++
Visual Studio(clang)代码格式化
Visual Studio(clang)代码格式化
174 0
|
存储 XML 数据安全/隐私保护
PyMuPDF 1.24.4 中文文档(八)(2)
PyMuPDF 1.24.4 中文文档(八)
1168 1
|
缓存 编解码 算法
Skia深入分析10——Skia库的性能与优化潜力
Skia库性能与优化潜力 图形/渲染 算法/架构 作为图形渲染引擎,性能上是非常重要的,按通常Android手机60帧的刷新率,绘制一帧的总时间只有16ms,可谓是毫厘必争。提升性能到最后,就必然跟不同CPU的特性打交道,毕竟一个SIMD下去,好做的提升5、6倍,不那么好做的也达到2、3倍,收益极其可观。 SIMD,在intel上是SSE,在arm上是neon,在
8911 0
|
C++
C++实现中文大写与阿拉伯数字的相互转换(类封装)
C++实现中文大写与阿拉伯数字的相互转换(类封装)
956 0
|
存储 JavaScript 小程序
Spring Event + DDD = 王炸!!上
Spring Event + DDD = 王炸!!上
|
C++ Windows
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
Visual Studio 安装检测内存工具-Visual Leak Detetctor。(适用于VS2013、VS2015、VS2017、VS2019、VS2022版本)
1083 0