import cv2 import numpy as np from PIL import Image Image.MAX_IMAGE_PIXELS = None import gradio as gr def freemium_watermark(img, sb_logo): width_px, height_px = img.size # Convert the logo to "RGBA" mode and resize it to 80% of the image width sb_logo = sb_logo.convert("RGBA") new_logo_width = int(width_px * 0.7) # 80% of the image width logo_ratio = new_logo_width / sb_logo.width new_logo_height = int(sb_logo.height * logo_ratio) sb_logo = sb_logo.resize((new_logo_width, new_logo_height)) # Make the logo semi-translucent transparent_img = Image.new('RGBA', (sb_logo.width, sb_logo.height), (0, 0, 0, 0)) # Create a transparent image of the same size sb_logo = Image.blend(transparent_img, sb_logo, alpha=0.3) # Blend the logo with the transparent image # Calculate the position to paste the logo at the center of the image paste_x_position = (width_px - new_logo_width) // 2 paste_y_position = int(height_px * 0.4) - (new_logo_height // 2) img.paste(sb_logo, (paste_x_position, paste_y_position), sb_logo) # Save the image return img sb_logo = Image.open("./SB_logo_horizontal.png") with gr.Blocks(analytics_enabled=False, theme=gr.themes.Soft()) as demo: with gr.Row(): with gr.Column(): input = gr.Image(type="pil", label="Upload Image", sources=["upload"]) gr.ClearButton(components=input) gr.Examples(examples=["hotel.jpg", "road_small.jpg", "preview_small.jpg", "trailer.jpg"], inputs=input) with gr.Column(): output = gr.Image(type = "filepath", label="Sketch Drawing", show_share_button=False) gr.Markdown("
Want to remove the watermark?
\n") gr.Markdown("Subscribe or purchase a model starting at $3.
") @gr.on(inputs=[input], outputs=[output], show_progress="minimal") def sketch(input_path): if input_path is None: return None img_array = np.array(input_path) # Line processing blurred = cv2.GaussianBlur(img_array, (7, 7), 0) edges = cv2.Canny(blurred, 100, 180) structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (4,3)) dilated_edges = cv2.dilate(edges, structuring_element, iterations=1) line_drawing = 255 - dilated_edges line_img = Image.fromarray(line_drawing) return freemium_watermark(line_img, sb_logo) demo.queue(default_concurrency_limit=10).launch()