Spaces:
Sleeping
Sleeping
Update app.py
Browse files12341 unmodified line6789101112131415161718192021222313 unmodified lines373839404142437 unmodified lines515253545556575859606162636465666768697071727374# Attempt to import cv2 and install opencv-python-headless if not availabletry:import cv2except ImportError:1 unmodified linesubprocess.check_call(["pip", "install", "opencv-python-headless"])import cv2from PIL import Imageimport numpy as npimport gradio as grimport os# Function to convert image to sketchdef convert_to_sketch(img, blur_strength, brightness, contrast):# Convert PIL Image to numpy arrayimg = np.array(img)# Convert the image to grayscaleimg_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)# Invert the grayscale imageimg_inverted = cv2.bitwise_not(img_gray)13 unmodified lines# Adjust brightness and contrastsketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)# Return the adjusted sketch imagereturn sketch_with_bg# Function to adjust brightness and contrast7 unmodified lines# Convert the input image to a sketch with adjustmentssketch = convert_to_sketch(image, blur_strength, brightness, contrast)# Convert numpy array to PIL Image before returningoutput_image = Image.fromarray(sketch)# Return the processed sketch image for displayreturn output_image# Create Gradio interfaceinterface = gr.Interface(fn=sketch_interface,inputs=[gr.Image(type="pil", label="Upload Image", image_mode="RGB"), # Support for various formatsgr.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 imagetitle="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.")# Launch the Gradio app12341 unmodified line6789101112131415161718192021222313 unmodified lines3738394041427 unmodified lines505152535455565758596061626364656667686970717273# Attempt to import opencv-python-headless and install it if not availabletry:import cv2except ImportError:1 unmodified linesubprocess.check_call(["pip", "install", "opencv-python-headless"])import cv2from PIL import Image, ImageTkimport numpy as npimport gradio as grimport os# Function to convert the image to a sketchdef convert_to_sketch(img, blur_strength, brightness, contrast):# Convert PIL Image to numpy arrayimg = np.array(img)# Convert the image to grayscaleimg_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Invert the grayscale imageimg_inverted = cv2.bitwise_not(img_gray)13 unmodified lines# Adjust brightness and contrastsketch_with_bg = adjust_brightness_contrast(sketch_with_bg, brightness, contrast)return sketch_with_bg# Function to adjust brightness and contrast7 unmodified lines# Convert the input image to a sketch with adjustmentssketch = convert_to_sketch(image, blur_strength, brightness, contrast)# Ensure the output is a valid image (PIL Image)output_image = Image.fromarray(sketch)# Return the processed sketch imagereturn output_image# Create Gradio interfaceinterface = gr.Interface(fn=sketch_interface,inputs=[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"),title="Cartoon to Sketch Converter",description="Upload an image to convert it into a sketch, adjust the blur strength, brightness, and contrast for different effects.")# Launch the Gradio app
app.py+9-10