Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
main.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pdf2image import convert_from_path
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def convert_pdf_to_webp(pdf_path):
|
| 8 |
+
images = convert_from_path(pdf_path)
|
| 9 |
+
if not os.path.exists('img'):
|
| 10 |
+
os.makedirs('img')
|
| 11 |
+
for i, image in enumerate(images):
|
| 12 |
+
image_path = os.path.join('img', f"page_{i}.webp")
|
| 13 |
+
image.save(image_path, "WEBP")
|
| 14 |
+
|
| 15 |
+
def convert_webp_to_pdf(output_pdf):
|
| 16 |
+
start_time = time.time()
|
| 17 |
+
image_files = [f for f in os.listdir('img') if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp'))]
|
| 18 |
+
if not image_files:
|
| 19 |
+
print("هیچ فایل تصویری در پوشه یافت نشد.")
|
| 20 |
+
return
|
| 21 |
+
image_files.sort()
|
| 22 |
+
image_paths = [os.path.join('img', f) for f in image_files]
|
| 23 |
+
images = [Image.open(image_path) for image_path in image_paths]
|
| 24 |
+
images[0].save(output_pdf, "PDF", resolution=100.0, save_all=True, append_images=images[1:])
|
| 25 |
+
print(f"فایل PDF با موفقیت ایجاد شد: {output_pdf}")
|
| 26 |
+
elapsed_time = time.time() - start_time
|
| 27 |
+
print(f"WebP to PDF conversion took {elapsed_time:.2f} seconds")
|
| 28 |
+
|
| 29 |
+
def convert_images_to_webp(image_paths):
|
| 30 |
+
if not os.path.exists('img'):
|
| 31 |
+
os.makedirs('img')
|
| 32 |
+
for image_path in image_paths:
|
| 33 |
+
try:
|
| 34 |
+
img = Image.open(image_path)
|
| 35 |
+
filename = os.path.basename(image_path)
|
| 36 |
+
output_filename = os.path.splitext(filename)[0] + '.webp'
|
| 37 |
+
output_path = os.path.join('img', output_filename)
|
| 38 |
+
img.save(output_path, 'WEBP')
|
| 39 |
+
print(f'Image {filename} converted to {output_filename}')
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f'Error converting {image_path}: {e}')
|
| 42 |
+
|
| 43 |
+
def filig(file_path):
|
| 44 |
+
file_size_bytes = os.path.getsize(file_path)
|
| 45 |
+
file_siz = file_size_bytes / (1024 * 1024)
|
| 46 |
+
return file_siz
|
| 47 |
+
|
| 48 |
+
def Percentage(file1,file2):
|
| 49 |
+
f = filig(file1) - filig(file2)
|
| 50 |
+
g = int((f / filig(file1)) * 100)
|
| 51 |
+
return str(g) + "%"
|
تست.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pip install streamlit pdf2image pillow
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from main import convert_pdf_to_webp, convert_webp_to_pdf, convert_images_to_webp,Percentage,filig
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
st.title("تبدیل فایلها")
|
| 9 |
+
st.write("فایل های PDF تا 80 درصد حجم کمتر!")
|
| 10 |
+
|
| 11 |
+
option = st.sidebar.selectbox("انتخاب عملیات", ("تبدیل تصاویر به PDF کم حجم", "کاهش حجم PDF"))
|
| 12 |
+
|
| 13 |
+
if option == "تبدیل تصاویر به PDF کم حجم":
|
| 14 |
+
st.subheader("تبدیل تصاویر به PDF کم حجم")
|
| 15 |
+
uploaded_files = st.file_uploader("تصاویر خود را آپلود کنید", type=["png", "jpg", "jpeg", "bmp", "gif"], accept_multiple_files=True)
|
| 16 |
+
|
| 17 |
+
if uploaded_files:
|
| 18 |
+
image_paths = []
|
| 19 |
+
kol = 0
|
| 20 |
+
for uploaded_file in uploaded_files:
|
| 21 |
+
image_path = os.path.join("temp", uploaded_file.name)
|
| 22 |
+
kol += filig(uploaded_file)
|
| 23 |
+
with open(image_path, "wb") as f:
|
| 24 |
+
f.write(uploaded_file.getbuffer())
|
| 25 |
+
image_paths.append(image_path)
|
| 26 |
+
|
| 27 |
+
if st.button("تبدیل به PDF کم حجم"):
|
| 28 |
+
convert_images_to_webp(image_paths)
|
| 29 |
+
output_pdf = "output.pdf"
|
| 30 |
+
convert_webp_to_pdf(output_pdf)
|
| 31 |
+
with open(output_pdf, "rb") as f:
|
| 32 |
+
st.download_button("دانلود PDF", f, file_name=output_pdf)
|
| 33 |
+
dar = Percentage(kol,output_pdf)
|
| 34 |
+
st.success(f"حجم PDF نسبت به حجم تصاویر {dar} کاهش یافته است!")
|
| 35 |
+
|
| 36 |
+
elif option == "کاهش حجم PDF":
|
| 37 |
+
st.subheader("کاهش حجم PDF")
|
| 38 |
+
uploaded_file = st.file_uploader("فایل PDF خود را آپلود کنید", type=["pdf"], accept_multiple_files=True)
|
| 39 |
+
|
| 40 |
+
if uploaded_file:
|
| 41 |
+
pdf_path = os.path.join("temp", uploaded_file.name)
|
| 42 |
+
with open(pdf_path, "wb") as f:
|
| 43 |
+
f.write(uploaded_file.getbuffer())
|
| 44 |
+
|
| 45 |
+
if st.button("کاهش حجم PDF"):
|
| 46 |
+
convert_pdf_to_webp(pdf_path)
|
| 47 |
+
output_pdf = "output_converted.pdf"
|
| 48 |
+
convert_webp_to_pdf(output_pdf)
|
| 49 |
+
with open(output_pdf, "rb") as f:
|
| 50 |
+
st.download_button("دانلود PDF", f, file_name=output_pdf)
|
| 51 |
+
dar = Percentage(uploaded_file,output_pdf)
|
| 52 |
+
st.success(f"حجم pdf {dar} کاهش یافت!")
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
if not os.path.exists('temp'):
|
| 56 |
+
os.makedirs('temp')
|
| 57 |
+
main()
|