BiliSakura commited on
Commit
d026ddf
·
verified ·
1 Parent(s): 3a2b896

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -26
app.py CHANGED
@@ -20,25 +20,42 @@ def _ensure_even(image: Image.Image) -> Image.Image:
20
  return image
21
 
22
 
23
- def _prepare_grayscale(image: Image.Image) -> np.ndarray:
24
- grayscale = image.convert("L")
25
- grayscale = _ensure_even(grayscale)
26
- width, height = grayscale.size
 
 
 
27
  if width < 2 or height < 2:
28
  raise gr.Error("Image must be at least 2x2 pixels after cropping.")
29
- return np.asarray(grayscale, dtype=np.float32)
30
 
31
 
 
32
  def _normalize_component(component: np.ndarray) -> np.ndarray:
33
- min_value = float(component.min())
34
- max_value = float(component.max())
35
- if max_value - min_value < 1e-8:
36
- return np.zeros_like(component, dtype=np.uint8)
37
- normalized = (component - min_value) / (max_value - min_value)
38
- return (normalized * 255).clip(0, 255).astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
 
41
  def haar_wavelet_components(image_array: np.ndarray) -> Dict[str, np.ndarray]:
 
42
  a = image_array[0::2, 0::2]
43
  b = image_array[0::2, 1::2]
44
  c = image_array[1::2, 0::2]
@@ -64,14 +81,15 @@ def compute_wavelet(
64
  if method is None:
65
  raise gr.Error(f"Unknown wavelet method: {method_name}")
66
 
67
- grayscale = _prepare_grayscale(image)
68
- components = method(grayscale)
69
 
70
  outputs: List[Image.Image] = []
71
  for key in COMPONENT_ORDER:
72
  component = components[key]
73
  normalized = _normalize_component(component)
74
- outputs.append(Image.fromarray(normalized, mode="L"))
 
75
  return tuple(outputs)
76
 
77
 
@@ -91,19 +109,11 @@ def build_demo() -> gr.Blocks:
91
  )
92
  run_button = gr.Button("Compute Wavelet")
93
  with gr.Row():
94
- ll_image = gr.Image(
95
- label="LL (Approximation)"
96
- )
97
- lh_image = gr.Image(
98
- label="LH (Vertical Details)"
99
- )
100
  with gr.Row():
101
- hl_image = gr.Image(
102
- label="HL (Horizontal Details)"
103
- )
104
- hh_image = gr.Image(
105
- label="HH (Diagonal Details)"
106
- )
107
 
108
  run_button.click(
109
  fn=compute_wavelet,
 
20
  return image
21
 
22
 
23
+ # 1. Renamed and updated to handle RGB
24
+ def _prepare_image(image: Image.Image) -> np.ndarray:
25
+ # Convert to RGB if necessary (e.g. RGBA or Grayscale input)
26
+ if image.mode != "RGB":
27
+ image = image.convert("RGB")
28
+ image = _ensure_even(image)
29
+ width, height = image.size
30
  if width < 2 or height < 2:
31
  raise gr.Error("Image must be at least 2x2 pixels after cropping.")
32
+ return np.asarray(image, dtype=np.float32)
33
 
34
 
35
+ # 2. Updated to support 3D arrays (RGB)
36
  def _normalize_component(component: np.ndarray) -> np.ndarray:
37
+ if component.ndim == 3:
38
+ # Normalize each channel independently to maximize visibility
39
+ normalized = np.zeros_like(component)
40
+ for i in range(3):
41
+ channel = component[:, :, i]
42
+ min_val = float(channel.min())
43
+ max_val = float(channel.max())
44
+ if max_val - min_val < 1e-8:
45
+ continue
46
+ normalized[:, :, i] = (channel - min_val) / (max_val - min_val)
47
+ return (normalized * 255).clip(0, 255).astype(np.uint8)
48
+ else:
49
+ min_value = float(component.min())
50
+ max_value = float(component.max())
51
+ if max_value - min_value < 1e-8:
52
+ return np.zeros_like(component, dtype=np.uint8)
53
+ normalized = (component - min_value) / (max_value - min_value)
54
+ return (normalized * 255).clip(0, 255).astype(np.uint8)
55
 
56
 
57
  def haar_wavelet_components(image_array: np.ndarray) -> Dict[str, np.ndarray]:
58
+ # NumPy broadcasting handles both 2D and 3D arrays automatically
59
  a = image_array[0::2, 0::2]
60
  b = image_array[0::2, 1::2]
61
  c = image_array[1::2, 0::2]
 
81
  if method is None:
82
  raise gr.Error(f"Unknown wavelet method: {method_name}")
83
 
84
+ img_array = _prepare_image(image) # Changed from grayscale
85
+ components = method(img_array)
86
 
87
  outputs: List[Image.Image] = []
88
  for key in COMPONENT_ORDER:
89
  component = components[key]
90
  normalized = _normalize_component(component)
91
+ # 3. Changed mode to RGB
92
+ outputs.append(Image.fromarray(normalized, mode="RGB"))
93
  return tuple(outputs)
94
 
95
 
 
109
  )
110
  run_button = gr.Button("Compute Wavelet")
111
  with gr.Row():
112
+ ll_image = gr.Image(label="LL (Approximation)")
113
+ lh_image = gr.Image(label="LH (Vertical Details)")
 
 
 
 
114
  with gr.Row():
115
+ hl_image = gr.Image(label="HL (Horizontal Details)")
116
+ hh_image = gr.Image(label="HH (Diagonal Details)")
 
 
 
 
117
 
118
  run_button.click(
119
  fn=compute_wavelet,