abiabidali commited on
Commit
845613a
·
verified ·
1 Parent(s): 0abd017

Update app.py

Browse files

the output images into a ZIP file

Files changed (1) hide show
  1. app.py +20 -28
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import torch
2
  from PIL import Image
3
  from RealESRGAN import RealESRGAN
@@ -5,12 +6,12 @@ import gradio as gr
5
  import numpy as np
6
  import tempfile
7
  import time
 
8
  import os
9
 
10
- # Set device to GPU if available, otherwise use CPU
11
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
 
13
- # Load the Real-ESRGAN model with specified scale
14
  def load_model(scale):
15
  model = RealESRGAN(device, scale=scale)
16
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
@@ -22,12 +23,11 @@ def load_model(scale):
22
  model.load_weights(weights_path, download=False)
23
  return model
24
 
25
- # Load different scales of the Real-ESRGAN model
26
  model2 = load_model(2)
27
  model4 = load_model(4)
28
  model8 = load_model(8)
29
 
30
- # Enhance the image using the specified scale
31
  def enhance_image(image, scale):
32
  try:
33
  print(f"Enhancing image with scale {scale}...")
@@ -49,7 +49,6 @@ def enhance_image(image, scale):
49
  print(f"Error enhancing image: {e}")
50
  return image
51
 
52
- # Adjust the DPI of the image
53
  def muda_dpi(input_image, dpi):
54
  dpi_tuple = (dpi, dpi)
55
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
@@ -58,7 +57,6 @@ def muda_dpi(input_image, dpi):
58
  temp_file.close()
59
  return Image.open(temp_file.name)
60
 
61
- # Resize the image to the specified width and height
62
  def resize_image(input_image, width, height):
63
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
64
  resized_image = image.resize((width, height))
@@ -67,12 +65,11 @@ def resize_image(input_image, width, height):
67
  temp_file.close()
68
  return Image.open(temp_file.name)
69
 
70
- # Process the images: enhance, adjust DPI, resize, and save
71
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
72
  processed_images = []
73
- file_paths = []
74
 
75
- for i, image_file in enumerate(image_files):
76
  input_image = np.array(Image.open(image_file).convert('RGB'))
77
  original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
78
 
@@ -84,24 +81,21 @@ def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width,
84
 
85
  if resize:
86
  original_image = resize_image(np.array(original_image), width, height)
87
-
88
- # Create a custom filename
89
- custom_filename = f"Image_Captioning_with_BLIP_{i+1}.jpg"
90
-
91
- # Save the image with the custom filename
92
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
93
- original_image.save(temp_file.name, format='JPEG')
94
 
95
- # Rename the file with the custom name
96
- final_path = temp_file.name.replace(temp_file.name.split('/')[-1], custom_filename)
97
- os.rename(temp_file.name, final_path)
98
-
99
- processed_images.append(original_image)
100
- file_paths.append(final_path)
 
 
 
 
 
101
 
102
- return processed_images, file_paths
103
 
104
- # Gradio interface setup
105
  iface = gr.Interface(
106
  fn=process_images,
107
  inputs=[
@@ -115,13 +109,11 @@ iface = gr.Interface(
115
  gr.Number(label="Height", value=512)
116
  ],
117
  outputs=[
118
- gr.Gallery(label="Final Images"), # Use gr.Gallery to display multiple images
119
- gr.Files(label="Download Final Images")
120
  ],
121
  title="Multi-Image Enhancer",
122
- description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results."
123
  )
124
 
125
- # Launch the Gradio interface
126
  iface.launch(debug=True)
127
 
 
1
+
2
  import torch
3
  from PIL import Image
4
  from RealESRGAN import RealESRGAN
 
6
  import numpy as np
7
  import tempfile
8
  import time
9
+ import zipfile
10
  import os
11
 
12
+ # Set the device to CUDA if available, otherwise CPU
13
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14
 
 
15
  def load_model(scale):
16
  model = RealESRGAN(device, scale=scale)
17
  weights_path = f'weights/RealESRGAN_x{scale}.pth'
 
23
  model.load_weights(weights_path, download=False)
24
  return model
25
 
26
+ # Load models for different scales
27
  model2 = load_model(2)
28
  model4 = load_model(4)
29
  model8 = load_model(8)
30
 
 
31
  def enhance_image(image, scale):
32
  try:
33
  print(f"Enhancing image with scale {scale}...")
 
49
  print(f"Error enhancing image: {e}")
50
  return image
51
 
 
52
  def muda_dpi(input_image, dpi):
53
  dpi_tuple = (dpi, dpi)
54
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
 
57
  temp_file.close()
58
  return Image.open(temp_file.name)
59
 
 
60
  def resize_image(input_image, width, height):
61
  image = Image.fromarray(input_image.astype('uint8'), 'RGB')
62
  resized_image = image.resize((width, height))
 
65
  temp_file.close()
66
  return Image.open(temp_file.name)
67
 
 
68
  def process_images(image_files, enhance, scale, adjust_dpi, dpi, resize, width, height):
69
  processed_images = []
70
+ temp_dir = tempfile.mkdtemp()
71
 
72
+ for image_file in image_files:
73
  input_image = np.array(Image.open(image_file).convert('RGB'))
74
  original_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
75
 
 
81
 
82
  if resize:
83
  original_image = resize_image(np.array(original_image), width, height)
 
 
 
 
 
 
 
84
 
85
+ # Save each image as JPEG
86
+ file_name = os.path.basename(image_file.name)
87
+ output_path = os.path.join(temp_dir, f"processed_{file_name}")
88
+ original_image.save(output_path, format='JPEG')
89
+ processed_images.append(output_path)
90
+
91
+ # Create a ZIP file with all processed images
92
+ zip_path = os.path.join(temp_dir, 'processed_images.zip')
93
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
94
+ for file_path in processed_images:
95
+ zipf.write(file_path, os.path.basename(file_path))
96
 
97
+ return zip_path
98
 
 
99
  iface = gr.Interface(
100
  fn=process_images,
101
  inputs=[
 
109
  gr.Number(label="Height", value=512)
110
  ],
111
  outputs=[
112
+ gr.File(label="Download Final Images (ZIP)") # Single file output for ZIP
 
113
  ],
114
  title="Multi-Image Enhancer",
115
+ description="Upload multiple images (.jpg, .png), enhance using AI, adjust DPI, resize, and download the final results as a ZIP file."
116
  )
117
 
 
118
  iface.launch(debug=True)
119