bluenevus commited on
Commit
fdc1d79
·
verified ·
1 Parent(s): 481213a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -14
app.py CHANGED
@@ -4,6 +4,13 @@ import requests
4
  import io
5
  import tempfile
6
  import sys
 
 
 
 
 
 
 
7
 
8
  def compress_pdf(input_file, url, strength):
9
  if input_file is None and (url is None or url.strip() == ""):
@@ -26,24 +33,49 @@ def compress_pdf(input_file, url, strength):
26
  reader = PyPDF2.PdfReader(pdf_content)
27
  writer = PyPDF2.PdfWriter()
28
 
29
- for i, page in enumerate(reader.pages):
30
- try:
31
- if strength == "High":
32
- page.compress_content_streams() # Maximum compression
33
- elif strength == "Medium":
34
- page.compress_content_streams() # Medium compression (same as high for now)
35
- else: # Low strength
36
- writer.add_page(page) # No compression
37
-
38
- if strength != "Low":
39
- writer.add_page(page)
40
- except Exception as e:
41
- return None, f"Error compressing page {i+1}: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
44
  writer.write(temp_file)
45
  temp_file_path = temp_file.name
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  return temp_file_path, "PDF compressed successfully!"
48
  except Exception as e:
49
  return None, f"Error compressing PDF: {str(e)}"
@@ -62,7 +94,7 @@ with gr.Blocks() as demo:
62
  with gr.Row():
63
  input_file = gr.File(label="Upload PDF")
64
  url_input = gr.Textbox(label="Or enter PDF URL")
65
- strength = gr.Radio(["High", "Medium", "Low"], label="Compression Strength", value="Medium", info="High: Maximum compression, Low: Least compression")
66
  compress_btn = gr.Button("Compress")
67
  output_file = gr.File(label="Download Compressed PDF")
68
  message = gr.Textbox(label="Message")
 
4
  import io
5
  import tempfile
6
  import sys
7
+ from PIL import Image
8
+
9
+ def compress_image(image, quality):
10
+ img_buffer = io.BytesIO()
11
+ image.save(img_buffer, format='JPEG', quality=quality)
12
+ img_buffer.seek(0)
13
+ return img_buffer
14
 
15
  def compress_pdf(input_file, url, strength):
16
  if input_file is None and (url is None or url.strip() == ""):
 
33
  reader = PyPDF2.PdfReader(pdf_content)
34
  writer = PyPDF2.PdfWriter()
35
 
36
+ if strength == "Low":
37
+ image_quality = 65
38
+ compression_level = 1
39
+ elif strength == "Medium":
40
+ image_quality = 40
41
+ compression_level = 2
42
+ else: # High
43
+ image_quality = 20
44
+ compression_level = 3
45
+
46
+ for page in reader.pages:
47
+ page.compress_content_streams() # Apply content stream compression
48
+
49
+ # Compress images on the page
50
+ for img_index, img in enumerate(page.images):
51
+ if img.image is not None:
52
+ try:
53
+ pil_image = Image.open(io.BytesIO(img.image))
54
+ compressed_image = compress_image(pil_image, image_quality)
55
+ page.replace_image(img_index, compressed_image)
56
+ except Exception as e:
57
+ print(f"Error compressing image: {e}")
58
+
59
+ writer.add_page(page)
60
 
61
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
62
  writer.write(temp_file)
63
  temp_file_path = temp_file.name
64
 
65
+ # Apply additional compression using PyPDF2's built-in compression
66
+ with open(temp_file_path, 'rb') as file:
67
+ reader = PyPDF2.PdfReader(file)
68
+ writer = PyPDF2.PdfWriter()
69
+
70
+ for page in reader.pages:
71
+ page.compress_content_streams()
72
+ writer.add_page(page)
73
+
74
+ writer.add_metadata(reader.metadata)
75
+
76
+ with open(temp_file_path, 'wb') as output_file:
77
+ writer.write(output_file)
78
+
79
  return temp_file_path, "PDF compressed successfully!"
80
  except Exception as e:
81
  return None, f"Error compressing PDF: {str(e)}"
 
94
  with gr.Row():
95
  input_file = gr.File(label="Upload PDF")
96
  url_input = gr.Textbox(label="Or enter PDF URL")
97
+ strength = gr.Radio(["Low", "Medium", "High"], label="Compression Strength", value="Medium", info="Low: ~25% compression, Medium: ~50% compression, High: ~75% compression")
98
  compress_btn = gr.Button("Compress")
99
  output_file = gr.File(label="Download Compressed PDF")
100
  message = gr.Textbox(label="Message")