lodhrangpt commited on
Commit
f2a3b89
·
verified ·
1 Parent(s): 147959f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -4,15 +4,6 @@ import io
4
  import tempfile
5
 
6
  def compress_image(input_image, output_format, quality=85):
7
- """
8
- Compress an image and return the compressed image as a file.
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
- Returns:
14
- - str: The path to the temporary file containing the compressed image.
15
- """
16
  img_byte_arr = io.BytesIO()
17
  if output_format.upper() in ['JPEG', 'JPG']:
18
  input_image.save(img_byte_arr, format='JPEG', quality=quality)
@@ -21,47 +12,70 @@ def compress_image(input_image, output_format, quality=85):
21
  elif output_format.upper() == 'WEBP':
22
  input_image.save(img_byte_arr, format='WEBP', quality=quality)
23
 
24
- # Save the compressed image to a temporary file
25
- img_byte_arr = img_byte_arr.getvalue() # Convert to bytes
26
  with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{output_format.lower()}') as temp_file:
27
  temp_file.write(img_byte_arr)
28
- temp_file_path = temp_file.name # Get the temporary file path
29
-
30
  return temp_file_path
31
 
32
  def image_compression_interface(uploaded_image, output_format, quality):
33
  try:
34
- # Open the image using PIL
35
  image = Image.open(uploaded_image)
36
-
37
- # Convert image to RGB if it has an alpha channel and the output format is JPEG, JPG, or WEBP
38
  if image.mode in ("RGBA", "P") and output_format.upper() in ['JPEG', 'JPG', 'WEBP']:
39
  image = image.convert("RGB")
40
 
41
- # Compress the image and save to a temporary file
42
  compressed_image_path = compress_image(image, output_format, quality)
43
-
44
- # Return the path to the compressed image file
45
  return compressed_image_path
46
 
47
  except Exception as e:
48
  return str(e)
49
 
50
- # Create the Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def create_gradio_interface():
52
- # Input components for Gradio (Updated for Gradio v3+)
53
- image_input = gr.File(label="Upload Image") # Changed to gr.File to handle raw file upload
54
  output_format_input = gr.Dropdown(choices=["JPEG", "JPG", "PNG", "WEBP"], label="Select Output Format")
55
  quality_input = gr.Slider(minimum=1, maximum=100, step=1, value=85, label="Select Compression Quality")
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- # Output components for Gradio (Updated for Gradio v3+)
58
- image_output = gr.File(label="Download Compressed Image") # Use gr.File for download
59
 
60
- # Set up the Gradio interface
61
  gr.Interface(
62
- fn=image_compression_interface,
63
  inputs=[image_input, output_format_input, quality_input],
64
- outputs=[image_output],
65
  ).launch()
66
 
67
  if __name__ == "__main__":
 
4
  import tempfile
5
 
6
  def compress_image(input_image, output_format, quality=85):
 
 
 
 
 
 
 
 
 
7
  img_byte_arr = io.BytesIO()
8
  if output_format.upper() in ['JPEG', 'JPG']:
9
  input_image.save(img_byte_arr, format='JPEG', quality=quality)
 
12
  elif output_format.upper() == 'WEBP':
13
  input_image.save(img_byte_arr, format='WEBP', quality=quality)
14
 
15
+ img_byte_arr = img_byte_arr.getvalue()
 
16
  with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{output_format.lower()}') as temp_file:
17
  temp_file.write(img_byte_arr)
18
+ temp_file_path = temp_file.name
19
+
20
  return temp_file_path
21
 
22
  def image_compression_interface(uploaded_image, output_format, quality):
23
  try:
 
24
  image = Image.open(uploaded_image)
 
 
25
  if image.mode in ("RGBA", "P") and output_format.upper() in ['JPEG', 'JPG', 'WEBP']:
26
  image = image.convert("RGB")
27
 
 
28
  compressed_image_path = compress_image(image, output_format, quality)
 
 
29
  return compressed_image_path
30
 
31
  except Exception as e:
32
  return str(e)
33
 
34
+ def get_current_size(compressed_image_path):
35
+ try:
36
+ with open(compressed_image_path, 'rb') as f:
37
+ size = len(f.read())
38
+ return f"Current Size: {size} bytes"
39
+ except FileNotFoundError:
40
+ return "Image not found"
41
+
42
+ def update_ui(uploaded_image, output_format, quality):
43
+ try:
44
+ image = Image.open(uploaded_image)
45
+ if image.mode in ("RGBA", "P") and output_format.upper() in ['JPEG', 'JPG', 'WEBP']:
46
+ image = image.convert("RGB")
47
+
48
+ compressed_image_path = compress_image(image, output_format, quality)
49
+ current_size = get_current_size(compressed_image_path)
50
+ return [compressed_image_path, current_size]
51
+
52
+ except Exception as e:
53
+ return [str(e), None]
54
+
55
  def create_gradio_interface():
56
+ image_input = gr.File(label="Upload Image")
 
57
  output_format_input = gr.Dropdown(choices=["JPEG", "JPG", "PNG", "WEBP"], label="Select Output Format")
58
  quality_input = gr.Slider(minimum=1, maximum=100, step=1, value=85, label="Select Compression Quality")
59
+ size_display = gr.Textbox(label="Current Size")
60
+
61
+ def on_quality_change(quality):
62
+ try:
63
+ image = Image.open(uploaded_image.value)
64
+ if image.mode in ("RGBA", "P") and output_format_input.value.upper() in ['JPEG', 'JPG', 'WEBP']:
65
+ image = image.convert("RGB")
66
+
67
+ compressed_image_path = compress_image(image, output_format_input.value, quality)
68
+ current_size = get_current_size(compressed_image_path)
69
+ return current_size
70
+ except Exception as e:
71
+ return str(e)
72
 
73
+ quality_input.change(on_quality_change, inputs=[quality_input], outputs=size_display)
 
74
 
 
75
  gr.Interface(
76
+ fn=update_ui,
77
  inputs=[image_input, output_format_input, quality_input],
78
+ outputs=[gr.File(label="Download Compressed Image"), size_display],
79
  ).launch()
80
 
81
  if __name__ == "__main__":