dkescape commited on
Commit
24f487e
·
verified ·
1 Parent(s): 90bcc4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -9,7 +9,7 @@ import gradio as gr
9
  import numpy as np
10
  from PIL import Image, ImageEnhance, ImageFilter
11
 
12
- # Load the model into memory to make running multiple predictions efficient
13
  img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
14
 
15
  def colorize_image(img_path):
@@ -23,10 +23,11 @@ def colorize_image(img_path):
23
 
24
  def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
25
  image = Image.open(img_path)
26
- enhancer_brightness = ImageEnhance.Brightness(image)
27
- image = enhancer_brightness.enhance(brightness)
28
- enhancer_contrast = ImageEnhance.Contrast(image)
29
- image = enhancer_contrast.enhance(contrast)
 
30
  if edge_enhance:
31
  image = image.filter(ImageFilter.EDGE_ENHANCE)
32
  temp_dir = tempfile.mkdtemp()
@@ -35,15 +36,18 @@ def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
35
  return enhanced_path
36
 
37
  def process_image(img_path, brightness, contrast, edge_enhance, output_format):
 
38
  colorized_path = colorize_image(img_path)
 
39
  enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
40
- # Convert to desired format
41
- image = Image.open(enhanced_path)
42
  temp_dir = tempfile.mkdtemp()
43
- output_filename = f'colorized_image.{output_format.lower()}'
44
- output_path = os.path.join(temp_dir, output_filename)
45
- image.save(output_path, format=output_format.upper())
46
- return (img_path, output_path), output_path
 
47
 
48
  title = "🌈 Color Restorization Model"
49
  description = "Upload a black & white photo to restore it in color using a deep learning model."
@@ -55,19 +59,27 @@ with gr.Blocks(title=title) as demo:
55
  with gr.Row():
56
  with gr.Column():
57
  input_image = gr.Image(type="filepath", label="Upload B&W Image", tool="editor")
58
- brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness", info="Adjust the brightness of the colorized image.")
59
- contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast", info="Adjust the contrast of the colorized image.")
60
- edge_enhance_checkbox = gr.Checkbox(label="Apply Edge Enhancement", info="Enhance edges to improve image detail.")
61
- output_format_dropdown = gr.Dropdown(choices=["PNG", "JPEG", "TIFF"], value="PNG", label="Output Format", info="Select the desired format for the output image.")
 
 
 
 
62
  submit_btn = gr.Button("Colorize")
63
  with gr.Column():
64
- comparison = gr.ImageSlider(label="Original vs Colorized")
 
 
 
 
65
  download_btn = gr.File(label="Download Colorized Image")
66
 
67
  submit_btn.click(
68
  fn=process_image,
69
  inputs=[input_image, brightness_slider, contrast_slider, edge_enhance_checkbox, output_format_dropdown],
70
- outputs=[comparison, download_btn]
71
  )
72
 
73
  demo.launch(enable_queue=True)
 
9
  import numpy as np
10
  from PIL import Image, ImageEnhance, ImageFilter
11
 
12
+ # Load the model once at startup for efficiency
13
  img_colorization = pipeline(Tasks.image_colorization, model='iic/cv_ddcolor_image-colorization')
14
 
15
  def colorize_image(img_path):
 
23
 
24
  def enhance_image(img_path, brightness=1.0, contrast=1.0, edge_enhance=False):
25
  image = Image.open(img_path)
26
+ # Adjust brightness
27
+ image = ImageEnhance.Brightness(image).enhance(brightness)
28
+ # Adjust contrast
29
+ image = ImageEnhance.Contrast(image).enhance(contrast)
30
+ # Optionally apply edge enhancement
31
  if edge_enhance:
32
  image = image.filter(ImageFilter.EDGE_ENHANCE)
33
  temp_dir = tempfile.mkdtemp()
 
36
  return enhanced_path
37
 
38
  def process_image(img_path, brightness, contrast, edge_enhance, output_format):
39
+ # Step 1: colorize
40
  colorized_path = colorize_image(img_path)
41
+ # Step 2: enhance (brightness / contrast / edge)
42
  enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
43
+ # Step 3: convert to chosen format
44
+ img = Image.open(enhanced_path)
45
  temp_dir = tempfile.mkdtemp()
46
+ filename = f'colorized_image.{output_format.lower()}'
47
+ output_path = os.path.join(temp_dir, filename)
48
+ img.save(output_path, format=output_format.upper())
49
+ # Return side-by-side (original, enhanced) plus the single downloadable file
50
+ return ([img_path, enhanced_path], output_path)
51
 
52
  title = "🌈 Color Restorization Model"
53
  description = "Upload a black & white photo to restore it in color using a deep learning model."
 
59
  with gr.Row():
60
  with gr.Column():
61
  input_image = gr.Image(type="filepath", label="Upload B&W Image", tool="editor")
62
+ brightness_slider = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
63
+ contrast_slider = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
64
+ edge_enhance_checkbox = gr.Checkbox(label="Apply Edge Enhancement")
65
+ output_format_dropdown = gr.Dropdown(
66
+ choices=["PNG", "JPEG", "TIFF"],
67
+ value="PNG",
68
+ label="Output Format"
69
+ )
70
  submit_btn = gr.Button("Colorize")
71
  with gr.Column():
72
+ comparison_gallery = gr.Gallery(
73
+ label="Original vs Colorized",
74
+ columns=2,
75
+ height="auto"
76
+ )
77
  download_btn = gr.File(label="Download Colorized Image")
78
 
79
  submit_btn.click(
80
  fn=process_image,
81
  inputs=[input_image, brightness_slider, contrast_slider, edge_enhance_checkbox, output_format_dropdown],
82
+ outputs=[comparison_gallery, download_btn]
83
  )
84
 
85
  demo.launch(enable_queue=True)