Spaces:
Sleeping
Sleeping
Update app.py
Browse files12 unmodified lines1314151617181920211 unmodified line2324252627282 unmodified lines313233343536373839404142434445464748495051523 unmodified lines56575859606162636465666768691 unmodified line7172737475763 unmodified lines8081828384853 unmodified lines899091929394959697989912 unmodified lines# Function to convert image to sketchdef convert_to_sketch(img, blur_strength, brightness, contrast):try:# Convert PIL Image to numpy array (BGR format for OpenCV)img = np.array(img)# Ensure blur_strength is oddblur_strength = max(1, int(blur_strength))if blur_strength % 2 == 0:1 unmodified line# Convert the image to grayscaleimg_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)# Invert the grayscale imageimg_inverted = 255 - img_gray2 unmodified linesimg_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), 0)# Blend the grayscale and blurred inverted images# Avoid division by zero by adding a small constantdenominator = 255 - img_blurdenominator[denominator == 0] = 1 # Prevent division by zeroimg_blend = cv2.multiply(img_gray, 256.0 / denominator)# Clip values to valid rangeimg_blend = np.clip(img_blend, 0, 255).astype(np.uint8)# Adjust brightness and contrastsketch_with_bg = adjust_brightness_contrast(img_blend, brightness, contrast)return sketch_with_bgexcept Exception as e:print(f"Error in convert_to_sketch: {str(e)}")return None# Function to adjust brightness and contrast3 unmodified linesbrightness = float(brightness)contrast = float(contrast)# Apply contrast firstimg = cv2.convertScaleAbs(img, alpha=contrast)# Apply brightnessif brightness > 0:img = cv2.add(img, np.ones_like(img) * brightness)else:img = cv2.subtract(img, np.ones_like(img) * abs(brightness))# Ensure output is in valid rangeimg = np.clip(img, 0, 255).astype(np.uint8)1 unmodified lineexcept Exception as e:print(f"Error in adjust_brightness_contrast: {str(e)}")return None# Gradio interface function3 unmodified linesreturn Nonetry:# Convert the input image to a sketch with adjustmentssketch = convert_to_sketch(image, blur_strength, brightness, contrast)3 unmodified lines# Convert the processed numpy array back to a PIL Imageoutput_image = Image.fromarray(sketch)return output_imageexcept Exception as e:print(f"Error in sketch_interface: {str(e)}")return None# Create Gradio interface12 unmodified lines13141516171819202122232425262728291 unmodified line313233343536372 unmodified lines404142434445464748495051525354555657585960616263643 unmodified lines68697071727374757677787980811 unmodified line83848586878889903 unmodified lines9495969798991001011023 unmodified lines10610710810911011111211311411511611711811912 unmodified lines# Function to convert image to sketchdef convert_to_sketch(img, blur_strength, brightness, contrast):try:# Add debug printprint(f"Input image shape: {np.array(img).shape}")# Convert PIL Image to numpy array (BGR format for OpenCV)img = np.array(img)# Check if image is emptyif img is None or img.size == 0:print("Error: Empty input image")return None# Ensure blur_strength is oddblur_strength = max(1, int(blur_strength))if blur_strength % 2 == 0:1 unmodified line# Convert the image to grayscaleimg_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)print(f"Grayscale shape: {img_gray.shape}")# Invert the grayscale imageimg_inverted = 255 - img_gray2 unmodified linesimg_blur = cv2.GaussianBlur(img_inverted, (blur_strength, blur_strength), 0)# Blend the grayscale and blurred inverted imagesdenominator = 255 - img_blurdenominator[denominator == 0] = 1 # Prevent division by zeroimg_blend = (img_gray * 256.0) / denominator# Clip values to valid rangeimg_blend = np.clip(img_blend, 0, 255).astype(np.uint8)print(f"Blend shape: {img_blend.shape}")# Adjust brightness and contrastsketch_with_bg = adjust_brightness_contrast(img_blend, brightness, contrast)print(f"Final output shape: {sketch_with_bg.shape}")return sketch_with_bgexcept Exception as e:print(f"Error in convert_to_sketch: {str(e)}")import tracebacktraceback.print_exc()return None# Function to adjust brightness and contrast3 unmodified linesbrightness = float(brightness)contrast = float(contrast)# Convert to float32 for calculationsimg = img.astype(float)# Apply contrastimg = img * contrast# Apply brightnessimg = img + brightness# Ensure output is in valid rangeimg = np.clip(img, 0, 255).astype(np.uint8)1 unmodified lineexcept Exception as e:print(f"Error in adjust_brightness_contrast: {str(e)}")import tracebacktraceback.print_exc()return None# Gradio interface function3 unmodified linesreturn Nonetry:print(f"Input image type: {type(image)}")print(f"Input image size: {image.size}")# Convert the input image to a sketch with adjustmentssketch = convert_to_sketch(image, blur_strength, brightness, contrast)3 unmodified lines# Convert the processed numpy array back to a PIL Imageoutput_image = Image.fromarray(sketch)print(f"Output image size: {output_image.size}")return output_imageexcept Exception as e:print(f"Error in sketch_interface: {str(e)}")import tracebacktraceback.print_exc()return None# Create Gradio interface
app.py+28-8