Spaces:
Build error
Build error
File size: 5,071 Bytes
935312a 57209d4 935312a 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 935312a 3d41f76 258c780 57209d4 935312a 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 258c780 3d41f76 935312a 3d41f76 |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import gradio as gr
import numpy as np
import cv2
filters = {
"Sepia": np.array([[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]]),
"Cool Blue": np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.889]]),
"Warm Orange": np.array([[1.0, 0.5, 0.0],
[0.5, 0.8, 0.2],
[0.2, 0.3, 0.5]]),
"Vintage": np.array([[0.6, 0.4, 0.2],
[0.3, 0.7, 0.3],
[0.2, 0.3, 0.5]]),
"Vintage Film": np.array([[0.7, 0.5, 0.3],
[0.4, 0.6, 0.2],
[0.2, 0.3, 0.5]]),
"Moonlight": np.array([[0.2, 0.4, 0.6],
[0.3, 0.5, 0.7],
[0.4, 0.6, 0.8]]),
"Desert Heat": np.array([[0.9, 0.7, 0.3],
[0.7, 0.5, 0.2],
[0.5, 0.3, 0.1]]),
"Purple Dream": np.array([[0.5, 0.3, 0.5],
[0.2, 0.4, 0.6],
[0.4, 0.2, 0.8]]),
"Forest Green": np.array([[0.2, 0.6, 0.1],
[0.3, 0.7, 0.2],
[0.1, 0.5, 0.3]]),
"Cyberpunk": np.array([[0.8, 0.3, 0.9],
[0.2, 0.7, 0.8],
[0.9, 0.2, 0.7]]),
"Pastel Dream": np.array([[0.8, 0.7, 0.9],
[0.7, 0.9, 0.8],
[0.9, 0.8, 0.7]]),
"Dramatic Contrast": np.array([[1.2, 0.0, 0.0],
[0.0, 1.2, 0.0],
[0.0, 0.0, 1.2]]),
"Faded Polaroid": np.array([[0.7, 0.6, 0.5],
[0.5, 0.6, 0.5],
[0.4, 0.5, 0.6]]),
"Neon Nights": np.array([[0.7, 0.2, 0.9],
[0.3, 0.8, 0.2],
[0.9, 0.3, 0.7]]),
"Underwater": np.array([[0.2, 0.4, 0.6],
[0.3, 0.5, 0.7],
[0.4, 0.6, 0.8]])
}
def apply_filter(image, filter_type):
if image is None:
return None
if filter_type in filters:
filtered_img = image.dot(filters[filter_type].T)
np.clip(filtered_img, 0, 255, out=filtered_img)
return filtered_img.astype(np.uint8)
elif filter_type == "High Contrast":
return cv2.convertScaleAbs(image, alpha=1.5, beta=0)
elif filter_type == "Grayscale":
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
elif filter_type == "Invert":
return 255 - image
elif filter_type == "Vignette":
rows, cols = image.shape[:2]
kernel_x = cv2.getGaussianKernel(cols, cols/4)
kernel_y = cv2.getGaussianKernel(rows, rows/4)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
vignette = np.copy(image)
for i in range(3):
vignette[:,:,i] = vignette[:,:,i] * mask
return vignette.astype(np.uint8)
else: # Original
return image
filter_options = ["Original"] + list(filters.keys()) + ["High Contrast", "Grayscale", "Invert", "Vignette"]
def update_filter_samples(image):
if image is None:
return None, gr.Gallery(), gr.Radio(choices=filter_options, value="Original")
filtered_images = [apply_filter(image, f) for f in filter_options]
return (filtered_images[0],
gr.Gallery(value=[(img, name) for img, name in zip(filtered_images, filter_options)]),
gr.Radio(choices=filter_options, value="Original"))
def update_output(image, filter_type):
if image is None or filter_type is None:
return None
return apply_filter(image, filter_type)
def gallery_select(evt: gr.SelectData, filter_choice):
if evt is None:
return filter_choice
return filter_options[evt.index]
with gr.Blocks() as demo:
gr.Markdown("# Color Filter Application")
gr.Markdown("Upload an image and apply various color filters to it.")
with gr.Row():
with gr.Column(scale=2):
input_image = gr.Image(label="Upload an image")
output_image = gr.Image(label="Filtered Image")
with gr.Column(scale=1):
filter_gallery = gr.Gallery(label="Filter Samples")
filter_choice = gr.Radio(
choices=filter_options,
value="Original",
label="Select a filter",
interactive=True
)
input_image.change(
fn=update_filter_samples,
inputs=[input_image],
outputs=[output_image, filter_gallery, filter_choice]
)
filter_gallery.select(
fn=gallery_select,
inputs=[filter_choice],
outputs=filter_choice
)
filter_choice.change(
fn=update_output,
inputs=[input_image, filter_choice],
outputs=output_image
)
demo.launch() |