gdTharusha commited on
Commit
18b1364
·
verified ·
1 Parent(s): b861bb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -64
app.py CHANGED
@@ -1,71 +1,74 @@
1
- import io
2
  import gradio as gr
3
- from PIL import Image
4
- import vtracer
5
- import tempfile
 
6
 
7
- def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
8
- color_precision, layer_difference, corner_threshold,
9
- length_threshold, max_iterations, splice_threshold, path_precision):
10
- """Converts an image to SVG using vtracer with customizable parameters."""
11
-
12
- # Convert Gradio image to bytes for vtracer compatibility
13
- img_byte_array = io.BytesIO()
14
- image.save(img_byte_array, format='PNG')
15
- img_bytes = img_byte_array.getvalue()
16
-
17
- # Perform the conversion
18
- svg_str = vtracer.convert_raw_image_to_svg(
19
- img_bytes,
20
- img_format='png',
21
- colormode=color_mode.lower(),
22
- hierarchical=hierarchical.lower(),
23
- mode=mode.lower(),
24
- filter_speckle=int(filter_speckle),
25
- color_precision=int(color_precision),
26
- layer_difference=int(layer_difference),
27
- corner_threshold=int(corner_threshold),
28
- length_threshold=float(length_threshold),
29
- max_iterations=int(max_iterations),
30
- splice_threshold=int(splice_threshold),
31
- path_precision=int(path_precision)
32
- )
33
-
34
- # Save the SVG string to a temporary file
35
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
36
- temp_file.write(svg_str.encode('utf-8'))
37
- temp_file.close()
38
-
39
- return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), temp_file.name
40
 
41
- # Gradio interface
42
- iface = gr.Blocks()
 
 
 
 
 
 
 
 
43
 
44
- with iface:
 
 
 
 
45
 
46
- gr.Interface(
47
- fn=convert_image,
48
- inputs=[
49
- gr.Image(type="pil", label="Upload Image"),
50
- gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
51
- gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
52
- gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
53
- gr.Slider(minimum=1, maximum=100, value=4, step=1, label="Filter Speckle"),
54
- gr.Slider(minimum=1, maximum=100, value=6, step=1, label="Color Precision"),
55
- gr.Slider(minimum=1, maximum=100, value=16, step=1, label="Layer Difference"),
56
- gr.Slider(minimum=1, maximum=100, value=60, step=1, label="Corner Threshold"),
57
- gr.Slider(minimum=1, maximum=100, value=4.0, step=0.5, label="Length Threshold"),
58
- gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Iterations"),
59
- gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold"),
60
- gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
61
- ],
62
- outputs=[
63
- gr.HTML(label="SVG Output"),
64
- gr.File(label="Download SVG")
65
- ],
66
- title="Convert Image to SVG vectors",
67
- description="Upload an image and customize the conversion parameters as needed.",
68
- )
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from torchvision import transforms
4
+ from PIL import Image, ImageEnhance
5
+ import rembg
6
 
7
+ # Upscaling function using a basic upscaling approach
8
+ def upscale_image(image, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0):
9
+ # Resize the image
10
+ width, height = image.size
11
+ new_size = (int(width * upscale_factor), int(height * upscale_factor))
12
+ upscaled_image = image.resize(new_size, Image.BICUBIC)
13
+
14
+ # Apply sharpness, contrast, and brightness adjustments
15
+ upscaled_image = ImageEnhance.Sharpness(upscaled_image).enhance(sharpness)
16
+ upscaled_image = ImageEnhance.Contrast(upscaled_image).enhance(contrast)
17
+ upscaled_image = ImageEnhance.Brightness(upscaled_image).enhance(brightness)
18
+
19
+ return upscaled_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Remastering function with background removal and enhancements
22
+ def remaster_image(image, remove_background=False, color_balance=1.0, detail_enhancement=1.0):
23
+ if remove_background:
24
+ image = rembg.remove(image)
25
+
26
+ # Apply color balance and detail enhancement
27
+ image = ImageEnhance.Color(image).enhance(color_balance)
28
+ image = ImageEnhance.Sharpness(image).enhance(detail_enhancement)
29
+
30
+ return image
31
 
32
+ # Main function combining upscaling and remastering
33
+ def process_image(image, apply_upscale=False, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0,
34
+ apply_remaster=False, remove_background=False, color_balance=1.0, detail_enhancement=1.0):
35
+ if apply_upscale:
36
+ image = upscale_image(image, upscale_factor, sharpness, contrast, brightness)
37
 
38
+ if apply_remaster:
39
+ image = remaster_image(image, remove_background, color_balance, detail_enhancement)
40
+
41
+ return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # Gradio UI
44
+ with gr.Blocks() as demo:
45
+ with gr.Row():
46
+ image_input = gr.Image(label="Upload Image", type="pil")
47
+ image_output = gr.Image(label="Output Image")
48
+
49
+ with gr.Row():
50
+ with gr.Group():
51
+ gr.Markdown("### Upscaling Options")
52
+ upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
53
+ upscale_factor = gr.Slider(1, 4, value=2, label="Upscale Factor")
54
+ sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness")
55
+ contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
56
+ brightness = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
57
+
58
+ with gr.Group():
59
+ gr.Markdown("### Remastering Options")
60
+ remaster_checkbox = gr.Checkbox(label="Apply Remastering")
61
+ remove_background = gr.Checkbox(label="Remove Background")
62
+ color_balance = gr.Slider(0.5, 2.0, value=1.0, label="Color Balance")
63
+ detail_enhancement = gr.Slider(0.5, 2.0, value=1.0, label="Detail Enhancement")
64
 
65
+ process_button = gr.Button("Process Image")
66
+
67
+ process_button.click(
68
+ process_image,
69
+ inputs=[image_input, upscale_checkbox, upscale_factor, sharpness, contrast, brightness,
70
+ remaster_checkbox, remove_background, color_balance, detail_enhancement],
71
+ outputs=image_output
72
+ )
73
+
74
+ demo.launch()