Spaces:
Running
Running
add-svg-png
#2
by
EduardoM102
- opened
- app.py +29 -3
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -25,7 +25,8 @@ from gradio_imageslider import ImageSlider
|
|
| 25 |
|
| 26 |
from huggingface_hub import hf_hub_download
|
| 27 |
import base64
|
| 28 |
-
from io import BytesIO
|
|
|
|
| 29 |
USE_TORCH_COMPILE = False
|
| 30 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
| 31 |
|
|
@@ -152,6 +153,30 @@ def create_hdr_effect(original_image, hdr):
|
|
| 152 |
hdr_image_8bit = np.clip(hdr_image * 255, 0, 255).astype('uint8')
|
| 153 |
return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
lazy_pipe = LazyLoadPipeline()
|
| 156 |
lazy_pipe.load()
|
| 157 |
|
|
@@ -193,8 +218,9 @@ def gradio_process_image(input_image, input_base64, resolution, num_inference_st
|
|
| 193 |
# Convert input_image and result to numpy arrays
|
| 194 |
input_array = np.array(input_image)
|
| 195 |
result_array = np.array(result)
|
| 196 |
-
|
| 197 |
-
|
|
|
|
| 198 |
|
| 199 |
title = """<h1 align="center">Image Upscaler with Tile Controlnet</h1>
|
| 200 |
<p align="center">The main ideas come from</p>
|
|
|
|
| 25 |
|
| 26 |
from huggingface_hub import hf_hub_download
|
| 27 |
import base64
|
| 28 |
+
from io import BytesIO, StringIO
|
| 29 |
+
import svgwrite
|
| 30 |
USE_TORCH_COMPILE = False
|
| 31 |
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
| 32 |
|
|
|
|
| 153 |
hdr_image_8bit = np.clip(hdr_image * 255, 0, 255).astype('uint8')
|
| 154 |
return Image.fromarray(cv2.cvtColor(hdr_image_8bit, cv2.COLOR_BGR2RGB))
|
| 155 |
|
| 156 |
+
def save_as_png(image):
|
| 157 |
+
"""Save image as PNG and return as base64."""
|
| 158 |
+
if isinstance(image, np.ndarray):
|
| 159 |
+
image = Image.fromarray(image)
|
| 160 |
+
|
| 161 |
+
buffered = BytesIO()
|
| 162 |
+
image.save(buffered, format="PNG")
|
| 163 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 164 |
+
return img_str
|
| 165 |
+
|
| 166 |
+
def vectorize_image(image):
|
| 167 |
+
svg_io = StringIO()
|
| 168 |
+
dwg = svgwrite.Drawing(size=(image.width, image.height))
|
| 169 |
+
for y in range(image.height):
|
| 170 |
+
for x in range(image.width):
|
| 171 |
+
r, g, b = image.getpixel((x, y))
|
| 172 |
+
color = f"rgb({r},{g},{b})"
|
| 173 |
+
dwg.add(dwg.rect(insert=(x, y), size=(1, 1), fill=color))
|
| 174 |
+
dwg.write(svg_io)
|
| 175 |
+
|
| 176 |
+
# Return the SVG as a response
|
| 177 |
+
svg_io.seek(0)
|
| 178 |
+
return svg_io.getvalue()
|
| 179 |
+
|
| 180 |
lazy_pipe = LazyLoadPipeline()
|
| 181 |
lazy_pipe.load()
|
| 182 |
|
|
|
|
| 218 |
# Convert input_image and result to numpy arrays
|
| 219 |
input_array = np.array(input_image)
|
| 220 |
result_array = np.array(result)
|
| 221 |
+
png_base64 = save_as_png(result)
|
| 222 |
+
svg = vectorize_image(result)
|
| 223 |
+
return [input_array, result_array, png_base64, svg]
|
| 224 |
|
| 225 |
title = """<h1 align="center">Image Upscaler with Tile Controlnet</h1>
|
| 226 |
<p align="center">The main ideas come from</p>
|
requirements.txt
CHANGED
|
@@ -12,4 +12,5 @@ peft
|
|
| 12 |
gradio
|
| 13 |
pillow
|
| 14 |
gradio-imageslider
|
| 15 |
-
pydantic==2.10.6
|
|
|
|
|
|
| 12 |
gradio
|
| 13 |
pillow
|
| 14 |
gradio-imageslider
|
| 15 |
+
pydantic==2.10.6
|
| 16 |
+
svgwrite
|