File size: 2,556 Bytes
5228307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
from main import convert_pdf_to_webp, convert_webp_to_pdf, convert_images_to_webp,Percentage,filig
import os
import shutil

def main():
    st.title("تبدیل فایل‌ها")
    st.write("فایل های PDF تا 80 درصد حجم کمتر!")

    option = st.sidebar.selectbox("انتخاب عملیات", ("تبدیل تصاویر به PDF کم حجم", "کاهش حجم PDF"))

    if option == "تبدیل تصاویر به PDF کم حجم":
        st.subheader("تبدیل تصاویر به PDF کم حجم")
        uploaded_files = st.file_uploader("تصاویر خود را آپلود کنید", type=["png", "jpg", "jpeg", "bmp", "gif"], accept_multiple_files=True)

        if uploaded_files:
            image_paths = []
            kol = 0
            for uploaded_file in uploaded_files:
                image_path = os.path.join("temp", uploaded_file.name)
                kol += filig(uploaded_file)
                with open(image_path, "wb") as f:
                    f.write(uploaded_file.getbuffer())
                image_paths.append(image_path)

            if st.button("تبدیل به PDF کم حجم"):
                convert_images_to_webp(image_paths)
                output_pdf = "output.pdf"
                convert_webp_to_pdf(output_pdf)
                with open(output_pdf, "rb") as f:
                    st.download_button("دانلود PDF", f, file_name=output_pdf)
                dar = Percentage(kol,output_pdf)
                st.success(f"حجم PDF نسبت به حجم تصاویر {dar} کاهش یافته است!")

    elif option == "کاهش حجم PDF":
        st.subheader("کاهش حجم PDF")
        uploaded_file = st.file_uploader("فایل PDF خود را آپلود کنید", type=["pdf"], accept_multiple_files=True)

        if uploaded_file:
            pdf_path = os.path.join("temp", uploaded_file.name)
            with open(pdf_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            if st.button("کاهش حجم PDF"):
                convert_pdf_to_webp(pdf_path)
                output_pdf = "output_converted.pdf"
                convert_webp_to_pdf(output_pdf)
                with open(output_pdf, "rb") as f:
                    st.download_button("دانلود PDF", f, file_name=output_pdf)
                dar = Percentage(uploaded_file,output_pdf)
                st.success(f"حجم pdf {dar} کاهش یافت!")

if __name__ == "__main__":
    if not os.path.exists('temp'):
        os.makedirs('temp')
    main()