Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2 as cv
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Filter Functions
|
| 6 |
+
def apply_filter(image, filter_type):
|
| 7 |
+
if filter_type == "Gray":
|
| 8 |
+
return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
|
| 9 |
+
elif filter_type == "Sepia":
|
| 10 |
+
sepia_filter = np.array([[0.272, 0.534, 0.131],
|
| 11 |
+
[0.349, 0.686, 0.168],
|
| 12 |
+
[0.393, 0.769, 0.189]])
|
| 13 |
+
sepia_image = cv.transform(image, sepia_filter)
|
| 14 |
+
return np.clip(sepia_image, 0, 255)
|
| 15 |
+
elif filter_type == "Invert":
|
| 16 |
+
return cv.bitwise_not(image)
|
| 17 |
+
elif filter_type == "Blur":
|
| 18 |
+
return cv.GaussianBlur(image, (15, 15), 0)
|
| 19 |
+
elif filter_type == "Sharpen":
|
| 20 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
| 21 |
+
return cv.filter2D(image, -1, kernel)
|
| 22 |
+
elif filter_type == "Edge Detection":
|
| 23 |
+
return cv.Canny(image, 100, 200)
|
| 24 |
+
else:
|
| 25 |
+
return image
|
| 26 |
+
|
| 27 |
+
# Rotation Function
|
| 28 |
+
def rotate_image(image, angle):
|
| 29 |
+
(h, w) = image.shape[:2]
|
| 30 |
+
center = (w // 2, h // 2)
|
| 31 |
+
matrix = cv.getRotationMatrix2D(center, angle, 1.0)
|
| 32 |
+
rotated_image = cv.warpAffine(image, matrix, (w, h))
|
| 33 |
+
return rotated_image
|
| 34 |
+
|
| 35 |
+
# Brightness and Contrast Adjustment
|
| 36 |
+
def adjust_brightness_contrast(image, brightness=0, contrast=0):
|
| 37 |
+
beta = brightness - 128
|
| 38 |
+
alpha = contrast / 128 + 1
|
| 39 |
+
adjusted = cv.convertScaleAbs(image, alpha=alpha, beta=beta)
|
| 40 |
+
return adjusted
|
| 41 |
+
|
| 42 |
+
# Resize Function
|
| 43 |
+
def resize_image(image, scale_percent):
|
| 44 |
+
width = int(image.shape[1] * scale_percent / 100)
|
| 45 |
+
height = int(image.shape[0] * scale_percent / 100)
|
| 46 |
+
resized = cv.resize(image, (width, height), interpolation=cv.INTER_AREA)
|
| 47 |
+
return resized
|
| 48 |
+
|
| 49 |
+
# Crop Function
|
| 50 |
+
def crop_image(image, x, y, width, height):
|
| 51 |
+
return image[y:y+height, x:x+width]
|
| 52 |
+
|
| 53 |
+
# Gradio function
|
| 54 |
+
def process_image(image, filter_type, rotation_angle, brightness, contrast, scale_percent, crop_x, crop_y, crop_width, crop_height):
|
| 55 |
+
image = np.array(image) # Convert Gradio image to OpenCV format
|
| 56 |
+
filtered_image = apply_filter(image, filter_type)
|
| 57 |
+
rotated_image = rotate_image(filtered_image, rotation_angle)
|
| 58 |
+
adjusted_image = adjust_brightness_contrast(rotated_image, brightness, contrast)
|
| 59 |
+
resized_image = resize_image(adjusted_image, scale_percent)
|
| 60 |
+
cropped_image = crop_image(resized_image, crop_x, crop_y, crop_width, crop_height)
|
| 61 |
+
return cropped_image
|
| 62 |
+
|
| 63 |
+
# Gradio Interface
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# Advanced Photo Editing Tool")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
filter_type = gr.Radio(["Gray", "Sepia", "Invert", "Original", "Blur", "Sharpen", "Edge Detection"], label="Filter Type")
|
| 72 |
+
rotation_angle = gr.Slider(0, 360, step=1, label="Rotation Angle")
|
| 73 |
+
brightness = gr.Slider(0, 255, step=1, label="Brightness")
|
| 74 |
+
contrast = gr.Slider(0, 255, step=1, label="Contrast")
|
| 75 |
+
scale_percent = gr.Slider(10, 200, step=10, label="Scale (%)")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
crop_x = gr.Slider(0, 500, step=1, label="Crop X")
|
| 79 |
+
crop_y = gr.Slider(0, 500, step=1, label="Crop Y")
|
| 80 |
+
crop_width = gr.Slider(10, 500, step=1, label="Crop Width")
|
| 81 |
+
crop_height = gr.Slider(10, 500, step=1, label="Crop Height")
|
| 82 |
+
|
| 83 |
+
image_output = gr.Image(label="Resulting Image")
|
| 84 |
+
gr.Button("Process").click(process_image, inputs=[image_input, filter_type, rotation_angle, brightness, contrast, scale_percent, crop_x, crop_y, crop_width, crop_height], outputs=image_output)
|
| 85 |
+
|
| 86 |
+
# Launch the demo
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|