razaAhmed commited on
Commit
8bcdfb3
·
1 Parent(s): 96fdf53

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirement.txt +45 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import io
4
+ import base64
5
+
6
+ def mb_to_kb_converter(image_path, output_path, target_size_kb):
7
+ try:
8
+ # Open the image file
9
+ with Image.open(image_path) as img:
10
+ # Calculate the target size in bytes
11
+ target_size_bytes = target_size_kb * 1024
12
+
13
+ # Resize the image while maintaining the aspect ratio
14
+ img.thumbnail((300, 300))
15
+
16
+ # Save the image with reduced quality to decrease file size
17
+ img.save(output_path, format="JPEG", quality=85)
18
+
19
+ # Ensure the image meets the target size constraint
20
+ while io.BytesIO().getbuffer().nbytes > target_size_bytes:
21
+ img.thumbnail((img.width // 2, img.height // 2))
22
+ img.save(output_path, format="JPEG", quality=85)
23
+
24
+ except Exception as e:
25
+ st.error(f"Error: {e}")
26
+ return None
27
+
28
+ return output_path
29
+
30
+ def main():
31
+ st.title("Image MB to KB Converter")
32
+
33
+ uploaded_file = st.file_uploader("Choose an image file (JPG or PNG)", type=["jpg", "jpeg", "png"])
34
+
35
+ if uploaded_file is not None:
36
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
37
+ st.write("")
38
+
39
+ # Set the target size in KB
40
+ target_size_kb = 100 # You can adjust this value
41
+
42
+ # Convert MB to KB and get the result as a BytesIO buffer
43
+ output_path = "converted_image.jpg"
44
+ converted_path = mb_to_kb_converter(uploaded_file, output_path, target_size_kb)
45
+
46
+ if converted_path is not None:
47
+ st.success("Conversion complete!")
48
+
49
+ # Encode the content of the buffer to base64 and then decode to UTF-8
50
+ with open(converted_path, "rb") as image_file:
51
+ b64_encoded = base64.b64encode(image_file.read()).decode("utf-8")
52
+
53
+
54
+
55
+ # Display the converted image
56
+ st.image(converted_path, caption="Converted Image", use_column_width=True)
57
+ # Display the download link for the converted image
58
+ st.markdown(
59
+ f'<a href="data:image/jpeg;base64,{b64_encoded}" download="converted_image.jpg">Download Converted Image</a>',
60
+ unsafe_allow_html=True
61
+ )
62
+ else:
63
+ st.error("Conversion failed. Please try again.")
64
+
65
+
66
+
67
+ if __name__ == "__main__":
68
+ main()
requirement.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.2.0
2
+ attrs==23.1.0
3
+ blinker==1.7.0
4
+ cachetools==5.3.2
5
+ certifi==2023.11.17
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ gitdb==4.0.11
9
+ GitPython==3.1.40
10
+ idna==3.6
11
+ importlib-metadata==6.11.0
12
+ Jinja2==3.1.2
13
+ jsonschema==4.20.0
14
+ jsonschema-specifications==2023.11.2
15
+ markdown-it-py==3.0.0
16
+ MarkupSafe==2.1.3
17
+ mdurl==0.1.2
18
+ numpy==1.26.2
19
+ packaging==23.2
20
+ pandas==2.1.4
21
+ Pillow==10.1.0
22
+ protobuf==4.25.1
23
+ pyarrow==14.0.1
24
+ pydeck==0.8.1b0
25
+ Pygments==2.17.2
26
+ python-dateutil==2.8.2
27
+ pytz==2023.3.post1
28
+ referencing==0.32.0
29
+ requests==2.31.0
30
+ rich==13.7.0
31
+ rpds-py==0.13.2
32
+ six==1.16.0
33
+ smmap==5.0.1
34
+ streamlit==1.29.0
35
+ tenacity==8.2.3
36
+ toml==0.10.2
37
+ toolz==0.12.0
38
+ tornado==6.4
39
+ typing_extensions==4.9.0
40
+ tzdata==2023.3
41
+ tzlocal==5.2
42
+ urllib3==2.1.0
43
+ validators==0.22.0
44
+ watchdog==3.0.0
45
+ zipp==3.17.0