YosrAbbassi commited on
Commit
f4de44c
1 Parent(s): 820c10f

Rename app1.py to app.py

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