Bubbles30 commited on
Commit
f7a751b
·
verified ·
1 Parent(s): 6af5c65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
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
- # Get original size
250
- nw, nh = img.size
251
 
252
- # Flatten any transparency
253
- if img.mode in ("RGBA", "LA"):
254
- bg = Image.new("RGB", (nw, nh), fill)
255
- bg.paste(img, mask=img.split()[-1])
256
- img = bg
 
257
 
258
  # Create square canvas
259
  canvas = Image.new("RGB", (target, target), fill)
260
- x = (target - nw) // 2
261
- y = (target - nh) // 2
262
- canvas.paste(img, (x, y))
 
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"