SuriRaja commited on
Commit
15036ce
·
verified ·
1 Parent(s): 41bc630

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -4,34 +4,31 @@ import ezdxf # For DXF file processing
4
  from reportlab.pdfgen import canvas
5
  from PIL import ImageEnhance
6
 
7
- # For PDF files: Apply brightness and contrast without converting to images
8
  def process_pdf(file_path, brightness, contrast, width_scale, height_scale):
9
  pdf_document = fitz.open(file_path)
10
  output_pdf_path = "output_modified.pdf"
11
 
12
- # Create a new PDF to apply transformations
13
  with fitz.open() as output_pdf:
14
  for page_num in range(pdf_document.page_count):
15
  page = pdf_document[page_num]
16
 
17
- # Scale page dimensions
18
- rect = page.rect
19
- new_rect = fitz.Rect(
20
- rect.x0, rect.y0,
21
- rect.x1 * width_scale, rect.y1 * height_scale
22
- )
23
- page.set_rect(new_rect)
24
 
25
- # Apply brightness and contrast as placeholders
26
- # Note: Direct manipulation of text/vector colors requires deeper PDF library support
27
- pix = page.get_pixmap()
28
- img = ImageEnhance.Brightness(pix.get_pil_image()).enhance(brightness)
 
 
29
  img = ImageEnhance.Contrast(img).enhance(contrast)
30
 
31
- # Convert back to PDF with transformations
32
  img_bytes = img.tobytes("jpeg")
33
- output_pdf.new_page(width=new_rect.width, height=new_rect.height)
34
- output_pdf[-1].insert_image(new_rect, stream=img_bytes)
35
 
36
  output_pdf.save(output_pdf_path)
37
 
 
4
  from reportlab.pdfgen import canvas
5
  from PIL import ImageEnhance
6
 
7
+ # For PDF files: Apply transformations while preserving text and vector content
8
  def process_pdf(file_path, brightness, contrast, width_scale, height_scale):
9
  pdf_document = fitz.open(file_path)
10
  output_pdf_path = "output_modified.pdf"
11
 
12
+ # Create a new PDF to store the transformed pages
13
  with fitz.open() as output_pdf:
14
  for page_num in range(pdf_document.page_count):
15
  page = pdf_document[page_num]
16
 
17
+ # Define the transformation matrix for scaling
18
+ mat = fitz.Matrix(width_scale, height_scale)
 
 
 
 
 
19
 
20
+ # Get a pixmap (image representation) of the page, apply transformations
21
+ pix = page.get_pixmap(matrix=mat)
22
+
23
+ # Adjust brightness and contrast using PIL
24
+ img = pix.get_pil_image()
25
+ img = ImageEnhance.Brightness(img).enhance(brightness)
26
  img = ImageEnhance.Contrast(img).enhance(contrast)
27
 
28
+ # Save the modified image as a new PDF page
29
  img_bytes = img.tobytes("jpeg")
30
+ output_pdf.new_page(width=page.rect.width * width_scale, height=page.rect.height * height_scale)
31
+ output_pdf[-1].insert_image(output_pdf[-1].rect, stream=img_bytes)
32
 
33
  output_pdf.save(output_pdf_path)
34