Spaces:
Sleeping
Sleeping
feat: added gradio
Browse files- .gitignore +3 -1
- app.py +73 -0
.gitignore
CHANGED
|
@@ -2,4 +2,6 @@ output
|
|
| 2 |
images
|
| 3 |
*.jpeg
|
| 4 |
*.pdf
|
| 5 |
-
*.png
|
|
|
|
|
|
|
|
|
| 2 |
images
|
| 3 |
*.jpeg
|
| 4 |
*.pdf
|
| 5 |
+
*.png
|
| 6 |
+
__pycache__
|
| 7 |
+
.gradio
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import fitz
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def parse_page_selection(selection):
|
| 8 |
+
pages = set()
|
| 9 |
+
for part in selection.split(','):
|
| 10 |
+
if '-' in part:
|
| 11 |
+
start, end = map(int, part.split('-'))
|
| 12 |
+
pages.update(range(start - 1, end)) # Convert to 0-based index
|
| 13 |
+
else:
|
| 14 |
+
pages.add(int(part) - 1) # Convert to 0-based index
|
| 15 |
+
return sorted(pages)
|
| 16 |
+
|
| 17 |
+
def invert_colors(pix):
|
| 18 |
+
img = np.frombuffer(pix.samples, dtype=np.uint8)
|
| 19 |
+
img = 255 - img # Invert colors
|
| 20 |
+
return fitz.Pixmap(pix.colorspace, pix.width, pix.height, img.tobytes(), pix.alpha)
|
| 21 |
+
|
| 22 |
+
def invert_pdf_pages(input_pdf, output_pdf, page_selection):
|
| 23 |
+
pages_to_invert = parse_page_selection(page_selection)
|
| 24 |
+
doc = fitz.open(input_pdf)
|
| 25 |
+
|
| 26 |
+
for page_num in pages_to_invert:
|
| 27 |
+
if 0 <= page_num < len(doc):
|
| 28 |
+
page = doc[page_num]
|
| 29 |
+
pix = page.get_pixmap()
|
| 30 |
+
inverted_pix = invert_colors(pix)
|
| 31 |
+
img_rect = page.rect
|
| 32 |
+
page.clean_contents()
|
| 33 |
+
page.insert_image(img_rect, stream=inverted_pix.tobytes())
|
| 34 |
+
|
| 35 |
+
doc.save(output_pdf)
|
| 36 |
+
doc.close()
|
| 37 |
+
|
| 38 |
+
def invert_pdf_document(input_file, page_selection):
|
| 39 |
+
# If input_file is a file-like object, write its content to a temp file.
|
| 40 |
+
# Otherwise, assume it's a file path.
|
| 41 |
+
if hasattr(input_file, "read"):
|
| 42 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_in:
|
| 43 |
+
tmp_in.write(input_file.read())
|
| 44 |
+
tmp_in_path = tmp_in.name
|
| 45 |
+
cleanup = True
|
| 46 |
+
else:
|
| 47 |
+
tmp_in_path = input_file
|
| 48 |
+
cleanup = False
|
| 49 |
+
|
| 50 |
+
# Create a temporary file to save the inverted PDF.
|
| 51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_out:
|
| 52 |
+
tmp_out_path = tmp_out.name
|
| 53 |
+
|
| 54 |
+
invert_pdf_pages(tmp_in_path, tmp_out_path, page_selection)
|
| 55 |
+
|
| 56 |
+
# Clean up temporary input file if it was created.
|
| 57 |
+
if cleanup:
|
| 58 |
+
os.remove(tmp_in_path)
|
| 59 |
+
return tmp_out_path
|
| 60 |
+
|
| 61 |
+
iface = gr.Interface(
|
| 62 |
+
fn=invert_pdf_document,
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.File(label="Input PDF"),
|
| 65 |
+
gr.Textbox(label="Page Selection", value="1-12,14-20,22-32,56,66-78,82-97")
|
| 66 |
+
],
|
| 67 |
+
outputs=gr.File(label="Output PDF"),
|
| 68 |
+
title="PDF Color Inverter",
|
| 69 |
+
description="Upload a PDF and specify the pages to invert colors."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
iface.launch(share=True)
|