Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def convert_to_grayscale(img):
|
| 7 |
if img is None:
|
| 8 |
return None
|
| 9 |
-
return img.convert('L').convert('RGB') #
|
| 10 |
|
| 11 |
-
# Function to apply
|
| 12 |
-
def apply_texture(img, texture
|
| 13 |
if img is None or texture is None:
|
| 14 |
return None
|
| 15 |
|
| 16 |
-
|
| 17 |
-
texture = texture.resize(img.size, Image.LANCZOS)
|
| 18 |
|
| 19 |
-
# Convert images to arrays
|
| 20 |
-
gray_arr = np.array(
|
| 21 |
texture_arr = np.array(texture, dtype=np.float32) / 255.0 # Normalize texture
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 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 |
-
#
|
| 38 |
-
|
| 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("##
|
| 49 |
|
| 50 |
with gr.Row():
|
| 51 |
input_img = gr.Image(label="Input Image", type="pil")
|
| 52 |
-
texture_img = gr.Image(label="Upload
|
| 53 |
|
| 54 |
-
|
| 55 |
-
contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast Adjustment")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
|
| 59 |
-
|
| 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
|
| 70 |
def process_contrast(img, contrast_level):
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|