jasvir-singh1021 commited on
Commit
3342fca
·
verified ·
1 Parent(s): e70a588

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -17
app.py CHANGED
@@ -1,32 +1,37 @@
 
 
 
 
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
25
  img = ImageEnhance.Contrast(img).enhance(contrast)
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":
@@ -36,9 +41,31 @@ def beautify_image(image, tint_color, contrast, brightness, saturation, apply_fi
36
  img_cv = cv2.transform(img_cv, kernel)
37
  img_cv = np.clip(img_cv, 0, 255)
38
  else:
39
- cmap = getattr(cv2, f'COLORMAP_{apply_filter.upper()}')
40
- img_cv = cv2.applyColorMap(cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY), cmap)
41
- img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
42
- img = Image.fromarray(img_cv)
 
 
 
43
 
44
  return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageEnhance, ImageOps, ImageColor
3
+ import numpy as np
4
+ import cv2
5
  import re
6
 
7
+ # Helper function to safely parse color
8
  def parse_color(color):
9
+ if isinstance(color, str):
10
+ if color.startswith("rgba"):
11
+ match = re.match(r"rgba\(([\d.]+), *([\d.]+), *([\d.]+), *[\d.]+\)", color)
12
+ if match:
13
+ r, g, b = match.groups()
14
+ return (int(float(r)), int(float(g)), int(float(b)))
15
+ elif color.startswith("#"):
16
+ return color # valid hex
17
+ return "white" # fallback
18
 
19
+ # Main image beautification function
20
  def beautify_image(image, tint_color, contrast, brightness, saturation, apply_filter):
21
  img = image.convert("RGB")
22
 
23
+ # Apply tint color if provided
24
  tint = parse_color(tint_color)
 
25
  if tint != "None":
26
  r, g, b = Image.new("RGB", img.size, tint).split()
27
  img = Image.blend(img, Image.merge("RGB", (r, g, b)), alpha=0.3)
28
 
29
+ # Enhance image attributes
30
  img = ImageEnhance.Contrast(img).enhance(contrast)
31
  img = ImageEnhance.Brightness(img).enhance(brightness)
32
  img = ImageEnhance.Color(img).enhance(saturation)
33
 
34
+ # Optional color filters
35
  if apply_filter != "None":
36
  img_cv = np.array(img)
37
  if apply_filter == "Sepia":
 
41
  img_cv = cv2.transform(img_cv, kernel)
42
  img_cv = np.clip(img_cv, 0, 255)
43
  else:
44
+ try:
45
+ cmap = getattr(cv2, f'COLORMAP_{apply_filter.upper()}')
46
+ img_cv = cv2.applyColorMap(cv2.cvtColor(img_cv, cv2.COLOR_RGB2GRAY), cmap)
47
+ img_cv = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
48
+ except AttributeError:
49
+ pass # invalid colormap, skip
50
+ img = Image.fromarray(img_cv.astype(np.uint8))
51
 
52
  return img
53
+
54
+ # Gradio UI
55
+ demo = gr.Interface(
56
+ fn=beautify_image,
57
+ inputs=[
58
+ gr.Image(type="pil", label="Upload Black & White Verse Image"),
59
+ gr.ColorPicker(label="Tint Color", value="#FFD700"),
60
+ gr.Slider(0.5, 2.0, 1.2, label="Contrast"),
61
+ gr.Slider(0.5, 2.0, 1.1, label="Brightness"),
62
+ gr.Slider(0.0, 2.0, 1.0, label="Saturation"),
63
+ gr.Dropdown(["None", "Sepia", "JET", "OCEAN", "PINK", "AUTUMN"], label="Optional Filter"),
64
+ ],
65
+ outputs=gr.Image(label="Beautified Image"),
66
+ title="🖌️ VerseGlow — Beautify Your B&W Verse Screenshots",
67
+ description="Upload black-and-white verse screenshots and make them visually beautiful with tint, contrast, and filters.",
68
+ )
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()