karthikmn commited on
Commit
bcf49d8
·
verified ·
1 Parent(s): ffe30b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -38
app.py CHANGED
@@ -1,13 +1,14 @@
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)
@@ -16,50 +17,56 @@ 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()
 
1
  import os
 
 
2
  import datetime
3
+ from PIL import Image
4
+ import gradio as gr
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
+ LOG_FILE = os.path.join(LOG_FOLDER, "conversion_log.txt")
12
 
13
  # Ensure required folders exist
14
  os.makedirs(INPUT_FOLDER, exist_ok=True)
 
17
 
18
  # Function to log conversion steps
19
  def log_conversion(status, input_image, output_image):
20
+ with open(LOG_FILE, "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
+ # Processing function
26
+ def process_image(uploaded_image, new_width, new_height):
27
+ try:
28
+ # Save the uploaded image temporarily
29
+ input_image_path = os.path.join(INPUT_FOLDER, "uploaded_image.png")
30
+ uploaded_image.save(input_image_path)
31
+
32
+ # Open and process the image
33
+ with Image.open(input_image_path) as img:
34
+ grayscale_image = img.convert("L") # Convert to grayscale
35
+ resized_image = grayscale_image.resize((int(new_width), int(new_height))) # Resize
36
+ resized_image.save(PROCESSED_IMAGE)
37
+
38
+ # Log success
39
+ log_conversion("Success", input_image_path, PROCESSED_IMAGE)
40
+
41
+ # Return processed image and log file
42
+ return PROCESSED_IMAGE, LOG_FILE
43
+ except Exception as e:
44
+ log_conversion("Failed", "Uploaded Image", "Processing Error")
45
+ return None, f"Error: {str(e)}"
46
 
47
+ # Gradio Interface
48
+ with gr.Blocks() as demo:
49
+ gr.Markdown("# Image Grayscale Converter and Resizer")
 
 
 
 
50
 
51
+ with gr.Row():
52
+ image_input = gr.Image(type="pil", label="Upload Image")
53
+ with gr.Column():
54
+ width_input = gr.Number(value=800, label="Width (pixels)")
55
+ height_input = gr.Number(value=600, label="Height (pixels)")
56
 
57
+ process_button = gr.Button("Process Image")
 
 
58
 
59
+ with gr.Row():
60
+ image_output = gr.Image(label="Processed Image")
61
+ log_file_output = gr.File(label="Download Log File")
 
 
 
 
 
62
 
63
+ # Define the button click functionality
64
+ process_button.click(
65
+ process_image,
66
+ inputs=[image_input, width_input, height_input],
67
+ outputs=[image_output, log_file_output],
68
+ )
69
 
70
+ # Launch the app
71
+ demo.launch()
 
 
 
 
 
72