DILSHAD737 commited on
Commit
40af5aa
·
verified ·
1 Parent(s): a7c8f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -52
app.py CHANGED
@@ -2,81 +2,82 @@ import gradio as gr
2
  from PIL import Image, ImageEnhance
3
  import numpy as np
4
 
5
- # Function to convert image to grayscale internally
 
 
 
 
 
 
 
6
  def convert_to_grayscale(img):
7
  if img is None:
8
  return None
9
- return img.convert('L').convert('RGB') # Convert to grayscale and back to RGB
10
 
11
- # Function to apply grayscale and custom texture with enhanced effect on white regions
12
- def apply_texture(img, texture, intensity=0.7):
13
  if img is None or texture is None:
14
  return None
15
 
16
- gray_img = convert_to_grayscale(img) # Convert input to grayscale
17
- texture = texture.resize(img.size, Image.LANCZOS) # Resize texture to match image
18
 
19
- # Convert images to arrays
20
- gray_arr = np.array(gray_img, dtype=np.float32) / 255.0 # Normalize grayscale image
21
  texture_arr = np.array(texture, dtype=np.float32) / 255.0 # Normalize texture
22
 
23
- # Detect white regions (where pixel intensity is high)
24
- white_mask = gray_arr.mean(axis=2) > 0.8 # If pixel intensity > 80% (near white)
25
-
26
- # Normal blending for non-white areas
27
- blended = np.where(texture_arr <= 0.5, 2 * gray_arr * texture_arr, 1 - 2 * (1 - gray_arr) * (1 - texture_arr))
28
-
29
- # Apply enhanced texture in white regions (90% texture, 10% grayscale)
30
- enhanced_texture = (0.9 * texture_arr + 0.1 * gray_arr)
31
-
32
- # Combine both approaches
33
- final_arr = np.where(white_mask[..., None], enhanced_texture, blended)
34
-
35
- return Image.fromarray((final_arr * 255).clip(0, 255).astype(np.uint8))
36
 
37
- # Function to adjust contrast
38
- def adjust_contrast(img, contrast_level):
39
- if img is None:
40
- return None
41
-
42
- enhancer = ImageEnhance.Contrast(img)
43
- return enhancer.enhance(contrast_level)
44
 
45
  # Gradio GUI
46
  def main():
47
  with gr.Blocks() as demo:
48
- gr.Markdown("## 🌫️ Enhanced Texture Effect with Contrast Adjustment")
49
 
50
  with gr.Row():
51
  input_img = gr.Image(label="Input Image", type="pil")
52
- texture_img = gr.Image(label="Upload Custom Texture", type="pil")
53
 
54
- intensity = gr.Slider(0.0, 1.0, value=0.6, label="Texture Strength")
55
- contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast Adjustment")
56
 
57
  with gr.Row():
58
- output_img1 = gr.Image(label="Grayscale + Texture Result", interactive=False)
59
- output_img2 = gr.Image(label="Final Image with Contrast", interactive=False)
60
-
61
- # Store intermediate texture result
62
- texture_state = gr.State()
63
-
64
- # Step 1: Apply texture with improved blending
65
- def process_texture(img, texture, intensity):
66
- result = apply_texture(img, texture, intensity)
67
- return result, result # Store for contrast adjustment
68
 
69
- # Step 2: Adjust contrast
70
  def process_contrast(img, contrast_level):
71
- return adjust_contrast(img, contrast_level)
72
-
73
- # Update texture-blended image
74
- input_img.change(process_texture, inputs=[input_img, texture_img, intensity], outputs=[output_img1, texture_state])
75
- texture_img.change(process_texture, inputs=[input_img, texture_img, intensity], outputs=[output_img1, texture_state])
76
- intensity.change(process_texture, inputs=[input_img, texture_img, intensity], outputs=[output_img1, texture_state])
77
-
78
- # Update final contrast image
79
- contrast.change(process_contrast, inputs=[texture_state, contrast], outputs=output_img2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  demo.launch()
82
 
 
2
  from PIL import Image, ImageEnhance
3
  import numpy as np
4
 
5
+ # Function to adjust contrast of the input image
6
+ def adjust_contrast(img, contrast_level):
7
+ if img is None:
8
+ return None
9
+ enhancer = ImageEnhance.Contrast(img)
10
+ return enhancer.enhance(contrast_level)
11
+
12
+ # Function to convert image to grayscale
13
  def convert_to_grayscale(img):
14
  if img is None:
15
  return None
16
+ return img.convert('L').convert('RGB') # Grayscale and back to RGB for compatibility
17
 
18
+ # Function to apply Multiply blending with texture (simulating print effect)
19
+ def apply_texture(img, texture):
20
  if img is None or texture is None:
21
  return None
22
 
23
+ # Resize texture to match image size
24
+ texture = texture.resize(img.size, Image.LANCZOS)
25
 
26
+ # Convert images to NumPy arrays
27
+ gray_arr = np.array(img, dtype=np.float32) / 255.0 # Normalize grayscale image
28
  texture_arr = np.array(texture, dtype=np.float32) / 255.0 # Normalize texture
29
 
30
+ # Apply Multiply blending: Multiply each pixel and normalize back to 0-255
31
+ blended = gray_arr * texture_arr
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ # Convert array back to image
34
+ return Image.fromarray((blended * 255).astype(np.uint8))
 
 
 
 
 
35
 
36
  # Gradio GUI
37
  def main():
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("## 🏡 Simulated Printing on Wood Texture (Multiply Blending)")
40
 
41
  with gr.Row():
42
  input_img = gr.Image(label="Input Image", type="pil")
43
+ texture_img = gr.Image(label="Upload Wood Texture", type="pil")
44
 
45
+ contrast = gr.Slider(0.0, 1.0, value=0.4, label="Contrast Adjustment")
 
46
 
47
  with gr.Row():
48
+ contrast_img = gr.Image(label="Contrast Adjusted Image", interactive=False)
49
+ grayscale_img = gr.Image(label="Grayscale Image", interactive=False)
50
+ output_img = gr.Image(label="Final Printed Effect", interactive=False)
 
 
 
 
 
 
 
51
 
52
+ # Step 1: Adjust contrast
53
  def process_contrast(img, contrast_level):
54
+ contrast_img = adjust_contrast(img, contrast_level)
55
+ return contrast_img
56
+
57
+ # Step 2: Convert contrast-adjusted image to grayscale
58
+ def process_grayscale(img, contrast_level):
59
+ contrast_img = adjust_contrast(img, contrast_level)
60
+ grayscale_img = convert_to_grayscale(contrast_img)
61
+ return contrast_img, grayscale_img
62
+
63
+ # Step 3: Apply Multiply blending with texture
64
+ def process_texture(img, texture, contrast_level):
65
+ contrast_img = adjust_contrast(img, contrast_level) # Adjust contrast first
66
+ grayscale_img = convert_to_grayscale(contrast_img) # Convert to grayscale
67
+ result = apply_texture(grayscale_img, texture) # Multiply blend with texture
68
+ return contrast_img, grayscale_img, result
69
+
70
+ # Update contrast-adjusted image
71
+ input_img.change(process_contrast, inputs=[input_img, contrast], outputs=contrast_img)
72
+
73
+ # Update grayscale image
74
+ input_img.change(process_grayscale, inputs=[input_img, contrast], outputs=[contrast_img, grayscale_img])
75
+ contrast.change(process_grayscale, inputs=[input_img, contrast], outputs=[contrast_img, grayscale_img])
76
+
77
+ # Update final processed image (Multiply blend with texture)
78
+ input_img.change(process_texture, inputs=[input_img, texture_img, contrast], outputs=[contrast_img, grayscale_img, output_img])
79
+ texture_img.change(process_texture, inputs=[input_img, texture_img, contrast], outputs=[contrast_img, grayscale_img, output_img])
80
+ contrast.change(process_texture, inputs=[input_img, texture_img, contrast], outputs=[contrast_img, grayscale_img, output_img])
81
 
82
  demo.launch()
83