Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
# Folder paths
|
| 7 |
+
INPUT_FOLDER = "./input/"
|
| 8 |
+
OUTPUT_FOLDER = "./output/"
|
| 9 |
+
LOG_FOLDER = "./logs/"
|
| 10 |
+
PROCESSED_IMAGE = os.path.join(OUTPUT_FOLDER, "processed_image.png")
|
| 11 |
+
|
| 12 |
+
# Ensure required folders exist
|
| 13 |
+
os.makedirs(INPUT_FOLDER, exist_ok=True)
|
| 14 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 15 |
+
os.makedirs(LOG_FOLDER, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# Function to log conversion steps
|
| 18 |
+
def log_conversion(status, input_image, output_image):
|
| 19 |
+
log_path = os.path.join(LOG_FOLDER, "conversion_log.txt")
|
| 20 |
+
with open(log_path, "a") as log_file:
|
| 21 |
+
log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
|
| 22 |
+
log_file.write(f"Input Image: {input_image}\n")
|
| 23 |
+
log_file.write(f"Output Image: {output_image}\n\n")
|
| 24 |
+
|
| 25 |
+
# Streamlit app interface
|
| 26 |
+
def main():
|
| 27 |
+
st.title("Image Grayscale Converter and Resizer")
|
| 28 |
+
|
| 29 |
+
# File upload
|
| 30 |
+
uploaded_file = st.file_uploader("Upload your image file", type=["jpg", "jpeg", "png"])
|
| 31 |
+
if uploaded_file:
|
| 32 |
+
input_image_path = os.path.join(INPUT_FOLDER, uploaded_file.name)
|
| 33 |
+
with open(input_image_path, "wb") as f:
|
| 34 |
+
f.write(uploaded_file.read())
|
| 35 |
+
st.success(f"File uploaded: {uploaded_file.name}")
|
| 36 |
+
|
| 37 |
+
# Display the uploaded image
|
| 38 |
+
st.image(input_image_path, caption="Uploaded Image", use_column_width=True)
|
| 39 |
+
|
| 40 |
+
# User controls for adjustments
|
| 41 |
+
new_width = st.number_input("Width (pixels)", value=800, step=10)
|
| 42 |
+
new_height = st.number_input("Height (pixels)", value=600, step=10)
|
| 43 |
+
|
| 44 |
+
# Process the file
|
| 45 |
+
if st.button("Process Image"):
|
| 46 |
+
try:
|
| 47 |
+
# Step 1: Open and process the image
|
| 48 |
+
with Image.open(input_image_path) as img:
|
| 49 |
+
grayscale_image = img.convert("L") # Convert to grayscale
|
| 50 |
+
resized_image = grayscale_image.resize((new_width, new_height)) # Resize
|
| 51 |
+
resized_image.save(PROCESSED_IMAGE)
|
| 52 |
+
|
| 53 |
+
# Step 2: Log the process
|
| 54 |
+
log_conversion("Success", input_image_path, PROCESSED_IMAGE)
|
| 55 |
+
|
| 56 |
+
# Provide download link
|
| 57 |
+
st.success("Image processed successfully!")
|
| 58 |
+
st.image(PROCESSED_IMAGE, caption="Processed Image", use_column_width=True)
|
| 59 |
+
st.download_button("Download Processed Image", open(PROCESSED_IMAGE, "rb"), file_name="processed_image.png")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
log_conversion("Failed", input_image_path, PROCESSED_IMAGE)
|
| 62 |
+
st.error(f"Error processing image: {e}")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|