Update app.py
Browse files
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
|
| 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
|
| 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 |
-
#
|
| 18 |
-
|
| 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 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
img = ImageEnhance.Contrast(img).enhance(contrast)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
img_bytes = img.tobytes("jpeg")
|
| 33 |
-
output_pdf.new_page(width=
|
| 34 |
-
output_pdf[-1].insert_image(
|
| 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 |
|