Spaces:
Sleeping
Sleeping
Update app.py
Browse files112341 unmodified line678991011121314141516171819202021222313 unmodified lines373839404041427 unmodified lines5051525453545557565758596061626463646566676968697170717273# Attempt to import cv2 and install opencv-python-headless if not available# Attempt to import opencv-python-headless and install it if not availabletry: import cv2except ImportError:1 unmodified line subprocess.check_call(["pip", "install", "opencv-python-headless"]) import cv2
from PIL import Imagefrom PIL import Image, ImageTkimport numpy as npimport gradio as grimport os
# Function to convert image to sketch# Function to convert the image to a sketchdef convert_to_sketch(img, blur_strength, brightness, contrast): # Convert PIL Image to numpy array img = np.array(img) # Convert the image to grayscale img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Invert the grayscale image img_inverted = cv2.bitwise_not(img_gray)13 unmodified lines # Adjust brightness and contrast sketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast) # Return the adjusted sketch image return sketch_with_bg
# Function to adjust brightness and contrast7 unmodified lines # Convert the input image to a sketch with adjustments sketch = convert_to_sketch(image, blur_strength, brightness, contrast) # Convert numpy array to PIL Image before returning # Ensure the output is a valid image (PIL Image) output_image = Image.fromarray(sketch) # Return the processed sketch image for display # Return the processed sketch image return output_image
# Create Gradio interfaceinterface = gr.Interface( fn=sketch_interface, inputs=[ gr.Image(type="pil", label="Upload Image", image_mode="RGB"), # Support for various formats gr.Image(type="pil", label="Upload Image"), gr.Slider(1, 51, step=2, label="Blur Strength", value=21), gr.Slider(-100, 100, label="Brightness", value=0), gr.Slider(1, 3, step=0.1, label="Contrast", value=1) ], outputs=[gr.Image(type="pil", label="Sketch Output")], # Output is now just the image outputs=gr.Image(type="pil", label="Sketch Output"), title="Cartoon to Sketch Converter", description="Upload an image (PNG, JPEG, or others) to convert it into a sketch. Adjust the blur strength, brightness, and contrast for different effects." description="Upload an image to convert it into a sketch, adjust the blur strength, brightness, and contrast for different effects.")
# Launch the Gradio appapp.py+9-10