gene commited on
Commit
02b2d80
·
1 Parent(s): dc07627

pdf to image

Browse files
Files changed (2) hide show
  1. app.py +51 -45
  2. requirements.txt +2 -3
app.py CHANGED
@@ -1,62 +1,68 @@
1
  import gradio as gr
2
- import cv2
3
- import numpy as np
4
  from PIL import Image
 
5
 
6
- def process_image(image):
7
  """
8
- Process the uploaded image and return basic analysis
9
  """
10
- if image is None:
11
- return "Please upload an image first.", None
12
 
13
- # Convert PIL image to numpy array
14
- img_array = np.array(image)
15
-
16
- # Basic image analysis
17
- height, width = img_array.shape[:2]
18
- channels = img_array.shape[2] if len(img_array.shape) > 2 else 1
19
-
20
- # Calculate basic statistics
21
- mean_color = np.mean(img_array, axis=(0, 1))
22
-
23
- # Create a simple edge detection visualization
24
- if len(img_array.shape) == 3:
25
- gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
26
- else:
27
- gray = img_array
28
-
29
- edges = cv2.Canny(gray, 50, 150)
30
- edges_colored = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
31
-
32
- # Create analysis text
33
- analysis_text = f"""
34
- **Image Analysis:**
35
- - Dimensions: {width} x {height} pixels
36
- - Channels: {channels}
37
- - Mean Color: R={mean_color[0]:.1f}, G={mean_color[1]:.1f}, B={mean_color[2]:.1f}
38
- """
39
-
40
- return analysis_text, edges_colored
 
 
41
 
42
  # Create the Gradio interface
43
- with gr.Blocks(title="Image Processor") as demo:
44
- gr.Markdown("# 🖼️ Image Processor")
45
- gr.Markdown("Upload an image to analyze it!")
46
 
47
  with gr.Row():
48
  with gr.Column():
49
- input_image = gr.Image(label="Upload Image", type="pil")
50
- process_btn = gr.Button("Process Image", variant="primary")
 
 
 
 
51
 
52
  with gr.Column():
53
- analysis_output = gr.Markdown(label="Analysis Results")
54
- processed_image = gr.Image(label="Processed Image (Edge Detection)")
55
 
56
- process_btn.click(
57
- fn=process_image,
58
- inputs=[input_image],
59
- outputs=[analysis_output, processed_image]
60
  )
61
 
62
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import fitz # PyMuPDF
 
3
  from PIL import Image
4
+ import io
5
 
6
+ def convert_pdf_to_image(pdf_file):
7
  """
8
+ Convert PDF to image(s) and return the first page as PIL Image
9
  """
10
+ if pdf_file is None:
11
+ return "Please upload a PDF file first.", None
12
 
13
+ try:
14
+ # Open the PDF file
15
+ pdf_document = fitz.open(pdf_file.name)
16
+
17
+ # Get the first page
18
+ first_page = pdf_document[0]
19
+
20
+ # Convert page to image (pixmap)
21
+ pix = first_page.get_pixmap(matrix=fitz.Matrix(2, 2)) # 2x zoom for better quality
22
+
23
+ # Convert pixmap to PIL Image
24
+ img_data = pix.tobytes("png")
25
+ img = Image.open(io.BytesIO(img_data))
26
+
27
+ # Close the PDF document
28
+ pdf_document.close()
29
+
30
+ # Get PDF info
31
+ pdf_info = f"""
32
+ **PDF Conversion Results:**
33
+ - Total pages: {len(pdf_document)}
34
+ - First page converted to image
35
+ - Image size: {img.width} x {img.height} pixels
36
+ - Format: PNG
37
+ """
38
+
39
+ return pdf_info, img
40
+
41
+ except Exception as e:
42
+ return f"Error processing PDF: {str(e)}", None
43
 
44
  # Create the Gradio interface
45
+ with gr.Blocks(title="PDF to Image Converter") as demo:
46
+ gr.Markdown("# 📄 PDF to Image Converter")
47
+ gr.Markdown("Upload a PDF file to convert the first page to an image!")
48
 
49
  with gr.Row():
50
  with gr.Column():
51
+ pdf_input = gr.File(
52
+ label="Upload PDF File",
53
+ file_types=[".pdf"],
54
+ file_count="single"
55
+ )
56
+ convert_btn = gr.Button("Convert to Image", variant="primary")
57
 
58
  with gr.Column():
59
+ conversion_output = gr.Markdown(label="Conversion Results")
60
+ converted_image = gr.Image(label="Converted Image (First Page)")
61
 
62
+ convert_btn.click(
63
+ fn=convert_pdf_to_image,
64
+ inputs=[pdf_input],
65
+ outputs=[conversion_output, converted_image]
66
  )
67
 
68
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,4 +1,3 @@
1
  gradio==5.49.0
2
- opencv-python==4.8.1.78
3
- Pillow==10.0.1
4
- numpy==1.24.3
 
1
  gradio==5.49.0
2
+ PyMuPDF==1.23.8
3
+ Pillow==10.0.1