Spaces:
Sleeping
Sleeping
yash bhaskar
commited on
Commit
·
26a3a6e
1
Parent(s):
2c9385f
vision model
Browse files
vision/Text_to_image/main.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# main.py
|
2 |
+
from textToPdf import create_pdf
|
3 |
+
from pdfToImage import pdf_to_image
|
4 |
+
|
5 |
+
def main():
|
6 |
+
# Sample input text
|
7 |
+
input_text = "This is a sample text that will be used to generate the PDF. " * 500 # Example long text
|
8 |
+
|
9 |
+
# Call the create_pdf function and get the path of the generated PDF
|
10 |
+
pdf_path = create_pdf(input_text)
|
11 |
+
image_path = pdf_to_image(pdf_path)
|
12 |
+
|
13 |
+
# Print the output path of the generated PDF
|
14 |
+
print(f"PDF generated successfully: {pdf_path}")
|
15 |
+
|
16 |
+
for i in image_path:
|
17 |
+
print(i)
|
18 |
+
|
19 |
+
if __name__ == "__main__":
|
20 |
+
main()
|
vision/Text_to_image/pdfToImage.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fitz # PyMuPDF
|
2 |
+
import os
|
3 |
+
|
4 |
+
def pdf_to_image(pdf_path, zoom=2.0):
|
5 |
+
# Open the PDF file
|
6 |
+
pdf_document = fitz.open(pdf_path)
|
7 |
+
|
8 |
+
# Create a list to store image paths
|
9 |
+
image_paths = []
|
10 |
+
|
11 |
+
# Create an 'Images' directory if it doesn't exist
|
12 |
+
os.makedirs("Images", exist_ok=True)
|
13 |
+
|
14 |
+
# Iterate over PDF pages and convert each to an image
|
15 |
+
for page_num in range(len(pdf_document)):
|
16 |
+
page = pdf_document.load_page(page_num) # Load the page
|
17 |
+
|
18 |
+
# Set zoom level to improve quality
|
19 |
+
mat = fitz.Matrix(zoom, zoom) # Create a transformation matrix with the zoom level
|
20 |
+
pix = page.get_pixmap(matrix=mat) # Render the page to an image with the specified zoom
|
21 |
+
|
22 |
+
image_file = f'Images/{os.path.basename(pdf_path)}_page_{page_num}.png'
|
23 |
+
pix.save(image_file) # Save the image as PNG
|
24 |
+
image_paths.append(image_file)
|
25 |
+
|
26 |
+
# Return the list containing paths of all images
|
27 |
+
return image_paths
|
28 |
+
|
29 |
+
# Example usage
|
30 |
+
# pdf_to_image('your_pdf_file.pdf', zoom=2.0) # Increase zoom for higher quality
|
vision/Text_to_image/textToPdf.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fpdf import FPDF
|
2 |
+
from datetime import datetime
|
3 |
+
import os
|
4 |
+
|
5 |
+
def create_pdf(input_text):
|
6 |
+
# Create instance of FPDF class
|
7 |
+
pdf = FPDF()
|
8 |
+
|
9 |
+
# Add a page
|
10 |
+
pdf.add_page()
|
11 |
+
|
12 |
+
# Set font
|
13 |
+
pdf.set_font("Arial", size=10)
|
14 |
+
|
15 |
+
# Split the input text into multiple lines if necessary
|
16 |
+
# This ensures that the text fits the page and multiple pages are handled
|
17 |
+
pdf.multi_cell(0, 5, txt=input_text)
|
18 |
+
|
19 |
+
# Create a unique file name with the current time
|
20 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
21 |
+
file_name = f"PDFs/Aditya_{timestamp}.pdf"
|
22 |
+
|
23 |
+
# Create output directory if it doesn't exist
|
24 |
+
os.makedirs(os.path.dirname(file_name), exist_ok=True)
|
25 |
+
|
26 |
+
# Save the PDF
|
27 |
+
pdf.output(file_name)
|
28 |
+
|
29 |
+
# Return the file path
|
30 |
+
return file_name
|