Spaces:
Sleeping
Sleeping
from pdf2image import convert_from_path | |
from PIL import Image | |
import os | |
import time | |
import shutil | |
def convert_pdf_to_webp(pdf_path): | |
images = convert_from_path(pdf_path) | |
if not os.path.exists('img'): | |
os.makedirs('img') | |
for i, image in enumerate(images): | |
image_path = os.path.join('img', f"page_{i}.webp") | |
image.save(image_path, "WEBP") | |
def convert_webp_to_pdf(output_pdf): | |
start_time = time.time() | |
image_files = [f for f in os.listdir('img') if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp'))] | |
if not image_files: | |
print("هیچ فایل تصویری در پوشه یافت نشد.") | |
return | |
image_files.sort() | |
image_paths = [os.path.join('img', f) for f in image_files] | |
images = [Image.open(image_path) for image_path in image_paths] | |
images[0].save(output_pdf, "PDF", resolution=100.0, save_all=True, append_images=images[1:]) | |
print(f"فایل PDF با موفقیت ایجاد شد: {output_pdf}") | |
elapsed_time = time.time() - start_time | |
print(f"WebP to PDF conversion took {elapsed_time:.2f} seconds") | |
def convert_images_to_webp(image_paths): | |
if not os.path.exists('img'): | |
os.makedirs('img') | |
for image_path in image_paths: | |
try: | |
img = Image.open(image_path) | |
filename = os.path.basename(image_path) | |
output_filename = os.path.splitext(filename)[0] + '.webp' | |
output_path = os.path.join('img', output_filename) | |
img.save(output_path, 'WEBP') | |
print(f'Image {filename} converted to {output_filename}') | |
except Exception as e: | |
print(f'Error converting {image_path}: {e}') | |
def filig(file_size): | |
return file_size / (1024 * 1024) | |
def Percentage(file1, file2): | |
f = filig(file1) - filig(file2) | |
g = int((f / filig(file1)) * 100) | |
return str(g) + "%" | |