YosrAbbassi commited on
Commit
cbfb11f
1 Parent(s): 9a5472e

Upload FINALFINAL.py

Browse files
Files changed (1) hide show
  1. FINALFINAL.py +147 -0
FINALFINAL.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz
3
+ import tkinter as tk
4
+ from tkinter import filedialog
5
+ from PIL import Image, ImageTk
6
+
7
+ class PDFViewer:
8
+ def __init__(self, pdf_path):
9
+ self.doc = fitz.open(pdf_path)
10
+ self.page = self.doc[0] # Assuming you want to work with the first page
11
+ self.page_num=0
12
+
13
+
14
+ # Get the size of the first page
15
+ self.page_width = int(self.page.rect.width)
16
+ self.page_height = int(self.page.rect.height)
17
+
18
+ # Create a Tkinter window
19
+ self.root = tk.Tk()
20
+ self.root.title("PDF Viewer")
21
+ self.root.attributes("-topmost", True) # Put the window at the top
22
+
23
+ # Create a canvas to display the PDF page
24
+ self.canvas = tk.Canvas(self.root, width=self.page_width, height=self.page_height)
25
+ self.canvas.pack()
26
+
27
+ # Initialize scrollbar
28
+ self.scrollbar = tk.Scrollbar(self.root, orient="vertical", command=self.on_scroll)
29
+ self.scrollbar.pack(side="right", fill="y")
30
+
31
+ self.canvas.configure(yscrollcommand=self.scrollbar.set)
32
+
33
+ # Display the first page
34
+ self.display_page()
35
+
36
+ # Bind mouse wheel event for scrolling
37
+ self.canvas.bind("<MouseWheel>", self.on_mousewheel)
38
+
39
+
40
+ # Display the PDF page on the canvas
41
+ pix = self.page.get_pixmap(matrix=fitz.Matrix(1, 1))
42
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
43
+ self.photo = ImageTk.PhotoImage(image=img)
44
+ self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
45
+
46
+ # Variables to store mouse click coordinates
47
+ self.start_x = None
48
+ self.start_y = None
49
+
50
+ # Bind left mouse button click and drag events
51
+ self.canvas.bind("<ButtonPress-1>", self.on_button_press)
52
+ self.canvas.bind("<B1-Motion>", self.on_move_press)
53
+ self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
54
+
55
+ # Initialize rectangle drawn on canvas
56
+ self.rect = None
57
+
58
+ def display_page(self):
59
+ # Clear canvas
60
+ self.canvas.delete("all")
61
+
62
+ # Get the size of the page
63
+ self.page = self.doc[self.page_num]
64
+ self.page_width = int(self.page.rect.width)
65
+ self.page_height = int(self.page.rect.height)
66
+
67
+ # Display the PDF page on the canvas
68
+ pix = self.page.get_pixmap()
69
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
70
+ self.photo = ImageTk.PhotoImage(image=img)
71
+ self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
72
+
73
+ # Update scrollbar
74
+ self.scrollbar.config(command=self.canvas.yview)
75
+ def on_scroll(self, *args):
76
+ # Update canvas view when scrollbar is moved
77
+ self.canvas.yview(*args)
78
+
79
+ def on_mousewheel(self, event):
80
+ # Scroll up/down when mouse wheel is moved
81
+ if event.delta < 0:
82
+ self.page_num += 1
83
+ else:
84
+ self.page_num -= 1
85
+
86
+ self.page_num = max(0, min(self.page_num, len(self.doc) - 1))
87
+ self.display_page()
88
+
89
+ def on_button_press(self, event):
90
+ # Record the starting point of the selection
91
+ self.start_x = self.canvas.canvasx(event.x)
92
+ self.start_y = self.canvas.canvasy(event.y)
93
+
94
+ # Delete any previously drawn rectangle
95
+ if self.rect:
96
+ self.canvas.delete(self.rect)
97
+
98
+ # Draw a new rectangle starting from the clicked point
99
+ self.rect = self.canvas.create_rectangle(self.start_x, self.start_y, self.start_x, self.start_y, outline='red')
100
+
101
+ def on_move_press(self, event):
102
+ # Update the size of the rectangle as the mouse moves
103
+ cur_x = self.canvas.canvasx(event.x)
104
+ cur_y = self.canvas.canvasy(event.y)
105
+
106
+ self.canvas.coords(self.rect, self.start_x, self.start_y, cur_x, cur_y)
107
+
108
+ def on_button_release(self, event):
109
+ # Save the selected area as an image
110
+ x1 = min(self.start_x, self.canvas.canvasx(event.x))
111
+ y1 = min(self.start_y, self.canvas.canvasy(event.y))
112
+ x2 = max(self.start_x, self.canvas.canvasx(event.x))
113
+ y2 = max(self.start_y, self.canvas.canvasy(event.y))
114
+
115
+ selected_area = fitz.Rect(x1, y1, x2, y2)
116
+ selected_pixmap = self.page.get_pixmap(matrix=fitz.Matrix(1, 1), clip=selected_area)
117
+
118
+ # Convert Pixmap to PIL Image
119
+ img = Image.frombytes("RGB", [selected_pixmap.width, selected_pixmap.height], selected_pixmap.samples)
120
+
121
+ # Save the selected area as an image
122
+ save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
123
+ if save_path:
124
+ img.save(save_path)
125
+
126
+ # Destroy the Tkinter window
127
+ self.root.destroy()
128
+
129
+ # Define the function to be called when the PDF file path is provided
130
+ def main(pdf_file):
131
+ # Ask user to select a PDF file
132
+ pdf_path = pdf_file.name
133
+ if pdf_path:
134
+ PDFViewer(pdf_path).root.mainloop()
135
+ return "File Saved"
136
+
137
+ pdf_file = gr.inputs.File(label="Select a PDF file")
138
+
139
+ # Create the Gradio interface
140
+ interface = gr.Interface(
141
+ fn=main,
142
+ inputs=pdf_file,
143
+ outputs="text",
144
+ title="PDF Region Extraction",
145
+ description="Select a region from a PDF file to extract.",
146
+ )
147
+ interface.launch()