wayandadang commited on
Commit
2ac62cb
1 Parent(s): de3361d

first commit

Browse files
Files changed (2) hide show
  1. app.py +76 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import io
4
+
5
+ def compress_image(input_image, output_format, quality=85):
6
+ """
7
+ Compress an image and return the compressed image.
8
+
9
+ Parameters:
10
+ - input_image (PIL.Image.Image): The input image.
11
+ - output_format (str): The format of the output image ('JPEG', 'JPG', 'PNG', or 'WEBP').
12
+ - quality (int, optional): The quality of the output image (1-100). Default is 85.
13
+
14
+ Returns:
15
+ - bytes: The compressed image in bytes.
16
+ """
17
+ img_byte_arr = io.BytesIO()
18
+ if output_format.upper() in ['JPEG', 'JPG']:
19
+ input_image.save(img_byte_arr, format='JPEG', quality=quality)
20
+ elif output_format.upper() == 'PNG':
21
+ input_image.save(img_byte_arr, format='PNG', compress_level=int((100-quality)/10))
22
+ elif output_format.upper() == 'WEBP':
23
+ input_image.save(img_byte_arr, format='WEBP', quality=quality)
24
+ img_byte_arr = img_byte_arr.getvalue()
25
+ return img_byte_arr
26
+
27
+ def main():
28
+ st.title("Image Compression App")
29
+
30
+ uploaded_file = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "webp"])
31
+
32
+ if uploaded_file is not None:
33
+ try:
34
+ # Display the uploaded image
35
+ image = Image.open(uploaded_file)
36
+ st.sidebar.image(image, caption="Uploaded Image", use_column_width=True)
37
+
38
+ # Get the size of the uploaded file
39
+ uploaded_file.seek(0, io.SEEK_END)
40
+ original_size = uploaded_file.tell()
41
+ uploaded_file.seek(0, io.SEEK_SET)
42
+ st.sidebar.write(f"Original size: {original_size / 1024:.2f} KB")
43
+
44
+ # Determine the default format based on the original file
45
+ original_format = image.format if image and image.format else 'JPEG'
46
+ if original_format.upper() not in ['JPEG', 'JPG', 'PNG', 'WEBP']:
47
+ original_format = 'JPEG'
48
+ output_format = st.sidebar.selectbox("Select output format", ['JPEG', 'JPG', 'PNG', 'WEBP'], index=['JPEG', 'JPG', 'PNG', 'WEBP'].index(original_format.upper()))
49
+
50
+ # Convert image to RGB if it has an alpha channel and the output format is JPEG, JPG, or WEBP
51
+ if image and image.mode in ("RGBA", "P") and output_format.upper() in ['JPEG', 'JPG', 'WEBP']:
52
+ image = image.convert("RGB")
53
+
54
+ quality = st.sidebar.slider("Select compression quality", 1, 100, 85)
55
+
56
+ # Display estimated size of compressed image
57
+ compressed_image_bytes = compress_image(image, output_format, quality)
58
+ compressed_size = len(compressed_image_bytes)
59
+ st.sidebar.write(f"Estimated compressed size: {compressed_size / 1024:.2f} KB")
60
+
61
+ if st.button("Compress Image"):
62
+ st.image(compressed_image_bytes, caption="Compressed Image", use_column_width=True)
63
+
64
+ # Determine file extension for download
65
+ file_extension = output_format.lower()
66
+ st.download_button(
67
+ label=f"Download Compressed Image.{file_extension.upper()}",
68
+ data=compressed_image_bytes,
69
+ file_name=f"compressed_image.{file_extension}",
70
+ mime=f"image/{file_extension}"
71
+ )
72
+ except Exception as e:
73
+ st.error(f"An error occurred: {e}")
74
+
75
+ if __name__ == "__main__":
76
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ pillow
3
+ io