Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -242,26 +242,29 @@ def resize_and_pad(img: Image.Image, max_size: int = 1000, fill=(255,255,255)) -
|
|
| 242 |
|
| 243 |
from PIL import ImageOps
|
| 244 |
|
| 245 |
-
def convert_to_jpeg(img: Image.Image, target: int = 1000, fill=(255,255,255)) -> Image.Image:
|
| 246 |
"""
|
| 247 |
Remove alpha by compositing over white, then pad to a target×target canvas.
|
| 248 |
"""
|
| 249 |
-
|
| 250 |
-
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
| 257 |
|
| 258 |
# Create square canvas
|
| 259 |
canvas = Image.new("RGB", (target, target), fill)
|
| 260 |
-
x = (target -
|
| 261 |
-
y = (target -
|
| 262 |
-
canvas.paste(
|
|
|
|
| 263 |
return canvas
|
| 264 |
|
|
|
|
| 265 |
def read_links_from_workbook(path):
|
| 266 |
ext = os.path.splitext(path)[1].lower()
|
| 267 |
engine = "pyxlsb" if ext == ".xlsb" else "openpyxl"
|
|
|
|
| 242 |
|
| 243 |
from PIL import ImageOps
|
| 244 |
|
| 245 |
+
def convert_to_jpeg(img: Image.Image, target: int = 1000, fill=(255, 255, 255)) -> Image.Image:
|
| 246 |
"""
|
| 247 |
Remove alpha by compositing over white, then pad to a target×target canvas.
|
| 248 |
"""
|
| 249 |
+
img = img.convert("RGBA") if img.mode != "RGBA" else img
|
| 250 |
+
r, g, b, a = img.split()
|
| 251 |
|
| 252 |
+
# Composite over white background
|
| 253 |
+
bg = Image.new("RGB", img.size, fill)
|
| 254 |
+
bg.paste(img, mask=a)
|
| 255 |
+
|
| 256 |
+
# Resize if needed
|
| 257 |
+
bg = resize_and_pad(bg, max_size=target, fill=fill)
|
| 258 |
|
| 259 |
# Create square canvas
|
| 260 |
canvas = Image.new("RGB", (target, target), fill)
|
| 261 |
+
x = (target - bg.width) // 2
|
| 262 |
+
y = (target - bg.height) // 2
|
| 263 |
+
canvas.paste(bg, (x, y))
|
| 264 |
+
|
| 265 |
return canvas
|
| 266 |
|
| 267 |
+
|
| 268 |
def read_links_from_workbook(path):
|
| 269 |
ext = os.path.splitext(path)[1].lower()
|
| 270 |
engine = "pyxlsb" if ext == ".xlsb" else "openpyxl"
|