File size: 4,452 Bytes
feca544
 
2d0e5bd
feca544
 
8ae827f
 
 
c345c18
feca544
 
 
74b0b86
feca544
 
 
 
 
c345c18
feca544
 
6ce6d58
feca544
 
 
 
8ae827f
 
 
 
 
 
 
 
6ce6862
8ae827f
6ce6862
8ae827f
 
c345c18
 
6ce6d58
8ae827f
 
 
 
 
 
 
 
fb1585d
8ae827f
 
 
 
 
 
c345c18
 
8ae827f
3300787
2d0e5bd
07040a2
722caa5
6ce6d58
9de6b46
5e3812c
07040a2
6ce6d58
8ae827f
818183b
 
c345c18
ccb6c7f
bbb6c56
8ae827f
 
5e3812c
6ce6d58
 
8ae827f
2d0e5bd
 
fa2c390
2d0e5bd
 
fb1585d
8ae827f
 
2d0e5bd
 
c345c18
 
2d0e5bd
8ae827f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import cv2
import numpy as np
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
import gradio as gr

sb_logo = Image.open("./SB_logo_horizontal.png")

def freemium_watermark(img, sb_logo, alpha):
    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=alpha)  # 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

def lighter(input_path):
    print('lighter')
    if input_path is None:
        return None
    img_array = np.array(input_path)
    # Line processing
    blurred = cv2.GaussianBlur(img_array, (7, 7), 0)
    #lighter
    edges = cv2.Canny(blurred, 150, 180)
    structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
    dilated_edges = cv2.dilate(edges, structuring_element, iterations=2)
    line_drawing = 255 - dilated_edges
    line_img = Image.fromarray(line_drawing)
    alpha = 0.3
    return freemium_watermark(line_img, sb_logo, alpha)
    
def darker(input_path):
    print('darker')
    if input_path is None:
        return None
    img_array = np.array(input_path)
    # Line processing
    blurred = cv2.GaussianBlur(img_array, (5, 5), 0)
    #darker
    edges = cv2.Canny(blurred, 70, 120) #darker
    #lighter
    # edges = cv2.Canny(blurred, 100, 180)
    structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
    dilated_edges = cv2.dilate(edges, structuring_element, iterations=2)
    line_drawing = 255 - dilated_edges
    line_img = Image.fromarray(line_drawing)
    alpha = 0.5
    return freemium_watermark(line_img, sb_logo, alpha)

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", "intersection.jpg", "road_small.jpg", "crash_small.jpg", "trailer.jpg"], inputs=input)
            # gr.Examples(examples=["hotel.jpg", "intersection.jpg", "road_small.jpg"], inputs=input)
        with gr.Column():
            output = gr.Image(type = "filepath", label="Sketch Drawing", show_share_button=False)
            with gr.Row():
                lighter_btn=gr.Button("Less Detail")
                darker_btn=gr.Button("More Detail")
                
            gr.Markdown("<p style='text-align: center; font-size: 20px;'>Want to remove the watermark, add a scale bar, and compass heading?</p>\n")
            gr.Markdown("<p style='text-align: center; font-size: 20px;'><a href='https://www.skyebrowse.com/pricing#sectionaboutpurchase'>Subscribe Here</a><p>")
            lighter_btn.click(lighter, inputs=[input], outputs=[output], show_progress="minimal")
            darker_btn.click(darker, inputs=[input], outputs=[output], show_progress="minimal")
    gr.Examples(examples=["hotel.jpg", "intersection.jpg", "road_small.jpg"], inputs=input)
    
    @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, 90, 150)
        structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
        dilated_edges = cv2.dilate(edges, structuring_element, iterations=2)
        line_drawing = 255 - dilated_edges
        line_img = Image.fromarray(line_drawing)
        alpha = 0.3
        return freemium_watermark(line_img, sb_logo, alpha)

demo.queue(default_concurrency_limit=10, api_open=False).launch(show_api=False)