DILSHAD737 commited on
Commit
ab569c8
·
verified ·
1 Parent(s): cf399eb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+
83
+ if __name__ == "__main__":
84
+ main()