ssh-AI commited on
Commit
b315605
·
verified ·
1 Parent(s): a78d2cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -34
app.py CHANGED
@@ -1,41 +1,21 @@
1
  import streamlit as st
2
  from PIL import Image, ImageEnhance
3
  import io
4
- from fpdf import FPDF
5
  from rembg import remove
6
 
7
- def enhance_image(img):
8
  # Remove background
9
- img_no_bg = remove(img)
10
-
11
- # Enhance color and sharpness
12
- img_pil = Image.open(io.BytesIO(img_no_bg)).convert("RGB")
13
- enhancer = ImageEnhance.Color(img_pil)
14
- img_pil = enhancer.enhance(1.3)
15
- enhancer = ImageEnhance.Sharpness(img_pil)
16
- img_pil = enhancer.enhance(2.0)
17
- return img_pil
18
-
19
- def images_to_pdf(images):
20
- pdf = FPDF(unit='mm', format='A4')
21
- for img in images:
22
- # Save to buffer
23
- buf = io.BytesIO()
24
- img.save(buf, format='JPEG')
25
- buf.seek(0)
26
- # Save temp file to load into pdf
27
- temp_path = '/tmp/temp_img.jpg'
28
- with open(temp_path, 'wb') as f:
29
- f.write(buf.read())
30
- pdf.add_page()
31
- pdf.image(temp_path, x=10, y=10, w=190)
32
- output = io.BytesIO()
33
- pdf.output(output)
34
- output.seek(0)
35
- return output
36
 
37
  st.set_page_config(page_title="Image Cleaner to PDF", page_icon="📄")
38
- st.title("🌍 Upload Images, Clean Backgrounds, Create PDF!")
39
 
40
  st.write("**Supported file types:** jpg, jpeg, png, webp")
41
 
@@ -45,16 +25,20 @@ if uploaded_files:
45
  cleaned_images = []
46
  with st.spinner('Processing images...'):
47
  for uploaded_file in uploaded_files:
48
- img = Image.open(uploaded_file)
49
- cleaned_img = enhance_image(uploaded_file.read())
50
  cleaned_images.append(cleaned_img)
51
 
52
  if cleaned_images:
53
- pdf_file = images_to_pdf(cleaned_images)
 
 
 
 
54
  st.success("Images cleaned and combined into PDF!")
55
  st.download_button(
56
  label="💾 Download PDF",
57
- data=pdf_file,
58
  file_name="cleaned_images.pdf",
59
  mime="application/pdf"
60
  )
 
1
  import streamlit as st
2
  from PIL import Image, ImageEnhance
3
  import io
 
4
  from rembg import remove
5
 
6
+ def enhance_image(image_bytes):
7
  # Remove background
8
+ img_no_bg = remove(image_bytes)
9
+ img = Image.open(io.BytesIO(img_no_bg)).convert("RGB")
10
+ # Enhance
11
+ enhancer = ImageEnhance.Color(img)
12
+ img = enhancer.enhance(1.3)
13
+ enhancer = ImageEnhance.Sharpness(img)
14
+ img = enhancer.enhance(2.0)
15
+ return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  st.set_page_config(page_title="Image Cleaner to PDF", page_icon="📄")
18
+ st.title("📄 Upload Images, Clean Backgrounds, Create PDF!")
19
 
20
  st.write("**Supported file types:** jpg, jpeg, png, webp")
21
 
 
25
  cleaned_images = []
26
  with st.spinner('Processing images...'):
27
  for uploaded_file in uploaded_files:
28
+ img_bytes = uploaded_file.read()
29
+ cleaned_img = enhance_image(img_bytes)
30
  cleaned_images.append(cleaned_img)
31
 
32
  if cleaned_images:
33
+ # Save images into a single PDF
34
+ pdf_bytes = io.BytesIO()
35
+ cleaned_images[0].save(pdf_bytes, format="PDF", save_all=True, append_images=cleaned_images[1:])
36
+ pdf_bytes.seek(0)
37
+
38
  st.success("Images cleaned and combined into PDF!")
39
  st.download_button(
40
  label="💾 Download PDF",
41
+ data=pdf_bytes,
42
  file_name="cleaned_images.pdf",
43
  mime="application/pdf"
44
  )