Spaces:
Running
Running
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.8) # 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 = (height_px - 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() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
input = gr.Image(type="pil", label="Upload Image", sources=["upload"]) | |
with gr.Column(): | |
image = gr.Image(label="Sketch Drawing", show_share_button=False) | |
examples = gr.Examples(examples=["road_small.jpg", "preview_small.jpg", "trailer.jpg"], inputs=[input], cache_examples=True) | |
def sketch(input_path): | |
if input_path is None: | |
return None | |
# with Image.open(input_path) as img: | |
# img_array = np.array(img) | |
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.launch() |