Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Apply color tint and enhancement
|
| 7 |
def beautify_image(image, tint_color, contrast, brightness, saturation, apply_filter):
|
| 8 |
img = image.convert("RGB")
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
img = Image.blend(img, Image.merge("RGB", (r, g, b)), alpha=0.3)
|
| 14 |
|
| 15 |
# Enhance image properties
|
|
@@ -17,7 +26,7 @@ def beautify_image(image, tint_color, contrast, brightness, saturation, apply_fi
|
|
| 17 |
img = ImageEnhance.Brightness(img).enhance(brightness)
|
| 18 |
img = ImageEnhance.Color(img).enhance(saturation)
|
| 19 |
|
| 20 |
-
# Apply filters
|
| 21 |
if apply_filter != "None":
|
| 22 |
img_cv = np.array(img)
|
| 23 |
if apply_filter == "Sepia":
|
|
@@ -33,21 +42,3 @@ def beautify_image(image, tint_color, contrast, brightness, saturation, apply_fi
|
|
| 33 |
img = Image.fromarray(img_cv)
|
| 34 |
|
| 35 |
return img
|
| 36 |
-
|
| 37 |
-
# Gradio UI
|
| 38 |
-
demo = gr.Interface(
|
| 39 |
-
fn=beautify_image,
|
| 40 |
-
inputs=[
|
| 41 |
-
gr.Image(type="pil", label="Upload Black & White Image"),
|
| 42 |
-
gr.ColorPicker(label="Tint Color", value="#FFD700"), # Golden yellow
|
| 43 |
-
gr.Slider(0.5, 2.0, 1.2, label="Contrast"),
|
| 44 |
-
gr.Slider(0.5, 2.0, 1.1, label="Brightness"),
|
| 45 |
-
gr.Slider(0.0, 2.0, 1.0, label="Saturation"),
|
| 46 |
-
gr.Dropdown(["None", "Sepia", "JET", "OCEAN", "PINK", "AUTUMN"], label="Optional Filter"),
|
| 47 |
-
],
|
| 48 |
-
outputs=gr.Image(label="Beautified Image"),
|
| 49 |
-
title="Beautify Verse Screenshots",
|
| 50 |
-
description="Upload B&W verse images to add color, contrast, and filters to make them beautiful.",
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
demo.launch()
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
def parse_color(color):
|
| 4 |
+
if isinstance(color, str) and color.startswith("rgba"):
|
| 5 |
+
# Parse rgba string
|
| 6 |
+
match = re.match(r"rgba\(([\d.]+), *([\d.]+), *([\d.]+), *[\d.]+\)", color)
|
| 7 |
+
if match:
|
| 8 |
+
r, g, b = match.groups()
|
| 9 |
+
return (int(float(r)), int(float(g)), int(float(b)))
|
| 10 |
+
elif isinstance(color, str) and color.startswith("#"):
|
| 11 |
+
return color # Hex color
|
| 12 |
+
return "white" # Fallback
|
| 13 |
|
|
|
|
| 14 |
def beautify_image(image, tint_color, contrast, brightness, saturation, apply_filter):
|
| 15 |
img = image.convert("RGB")
|
| 16 |
+
|
| 17 |
+
# Handle rgba or hex color from color picker
|
| 18 |
+
tint = parse_color(tint_color)
|
| 19 |
+
|
| 20 |
+
if tint != "None":
|
| 21 |
+
r, g, b = Image.new("RGB", img.size, tint).split()
|
| 22 |
img = Image.blend(img, Image.merge("RGB", (r, g, b)), alpha=0.3)
|
| 23 |
|
| 24 |
# Enhance image properties
|
|
|
|
| 26 |
img = ImageEnhance.Brightness(img).enhance(brightness)
|
| 27 |
img = ImageEnhance.Color(img).enhance(saturation)
|
| 28 |
|
| 29 |
+
# Apply filters
|
| 30 |
if apply_filter != "None":
|
| 31 |
img_cv = np.array(img)
|
| 32 |
if apply_filter == "Sepia":
|
|
|
|
| 42 |
img = Image.fromarray(img_cv)
|
| 43 |
|
| 44 |
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|