tl / app.py
attendantelectro's picture
Rename تست.py to app.py
c05b433 verified
raw
history blame
2.56 kB
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()