Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files- app.py +88 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
def apply_image_transformations(img, transform, scale_factor=1.0):
|
| 8 |
+
"""Apply various image transformations"""
|
| 9 |
+
img = np.array(img)
|
| 10 |
+
|
| 11 |
+
if transform == "cartoon":
|
| 12 |
+
# Apply cartoon effect
|
| 13 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 14 |
+
gray = cv2.medianBlur(gray, 5)
|
| 15 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 16 |
+
cv2.THRESH_BINARY, 9, 9)
|
| 17 |
+
|
| 18 |
+
color = cv2.bilateralFilter(img, 9, 300, 300)
|
| 19 |
+
return cv2.bitwise_and(color, color, mask=edges)
|
| 20 |
+
|
| 21 |
+
elif transform == "oil_painting":
|
| 22 |
+
# Apply oil painting effect
|
| 23 |
+
res = cv2.xphoto.oilPainting(img, 7, 1)
|
| 24 |
+
return res
|
| 25 |
+
|
| 26 |
+
elif transform == "pixelate":
|
| 27 |
+
# Pixelation effect
|
| 28 |
+
height, width = img.shape[:2]
|
| 29 |
+
small = cv2.resize(img, (32, 32), interpolation=cv2.INTER_LINEAR)
|
| 30 |
+
return cv2.resize(small, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 31 |
+
|
| 32 |
+
elif transform == "sketch":
|
| 33 |
+
# Pencil sketch effect
|
| 34 |
+
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 35 |
+
inverted = cv2.bitwise_not(gray_img)
|
| 36 |
+
blurred = cv2.GaussianBlur(inverted, (21, 21), 0)
|
| 37 |
+
inverted_blurred = cv2.bitwise_not(blurred)
|
| 38 |
+
return cv2.divide(gray_img, inverted_blurred, scale=256.0)
|
| 39 |
+
|
| 40 |
+
return img # Return original if no transformation selected
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 44 |
+
gr.Markdown("# 🖼️ Professional Image Transformer")
|
| 45 |
+
gr.Markdown("Upload your photo and apply various artistic transformations")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 50 |
+
transform_type = gr.Radio(
|
| 51 |
+
choices=["cartoon", "oil_painting", "pixelate", "sketch"],
|
| 52 |
+
value="cartoon",
|
| 53 |
+
label="Transformation Style"
|
| 54 |
+
)
|
| 55 |
+
transform_btn = gr.Button("Apply Transformation", variant="primary")
|
| 56 |
+
|
| 57 |
+
with gr.Column(scale=2):
|
| 58 |
+
output_image = gr.Image(label="Transformed Image", interactive=False)
|
| 59 |
+
|
| 60 |
+
# Examples gallery
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=[
|
| 63 |
+
["examples/portrait1.jpg", "cartoon"],
|
| 64 |
+
["examples/landscape1.jpg", "oil_painting"],
|
| 65 |
+
["examples/group1.jpg", "pixelate"]
|
| 66 |
+
],
|
| 67 |
+
inputs=[image_input, transform_type],
|
| 68 |
+
outputs=output_image,
|
| 69 |
+
fn=apply_image_transformations,
|
| 70 |
+
cache_examples=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Transformation function
|
| 74 |
+
transform_btn.click(
|
| 75 |
+
fn=apply_image_transformations,
|
| 76 |
+
inputs=[image_input, transform_type],
|
| 77 |
+
outputs=output_image,
|
| 78 |
+
api_name="transform"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Launch configuration
|
| 82 |
+
demo.launch(
|
| 83 |
+
title="Image Transformer Pro",
|
| 84 |
+
css="footer {visibility: hidden}",
|
| 85 |
+
footer_links=[
|
| 86 |
+
{"label": "Built with AnyCoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 87 |
+
]
|
| 88 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow
|
| 2 |
+
cv2
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|