multimodalart HF staff commited on
Commit
85875c3
1 Parent(s): cafc712

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -18,20 +18,27 @@ pipe.load_lora_weights(
18
  weight_name="visual-identity-design.safetensors"
19
  )
20
 
21
- def square_center_crop(img, target_size=768):
22
  if img.mode in ('RGBA', 'P'):
23
  img = img.convert('RGB')
24
-
25
- width, height = img.size
 
 
 
 
26
  crop_size = min(width, height)
27
-
 
28
  left = (width - crop_size) // 2
29
  top = (height - crop_size) // 2
30
- right = left + crop_size
31
- bottom = top + crop_size
32
-
33
- img_cropped = img.crop((left, top, right, bottom))
34
- return img_cropped.resize((target_size, target_size), Image.Resampling.LANCZOS)
 
 
35
 
36
  def duplicate_horizontally(img):
37
  # Convert PIL Image to numpy array
 
18
  weight_name="visual-identity-design.safetensors"
19
  )
20
 
21
+ def square_center_crop_numpy(img, target_size=768):
22
  if img.mode in ('RGBA', 'P'):
23
  img = img.convert('RGB')
24
+
25
+ # Convert PIL image to numpy array
26
+ img_array = np.array(img)
27
+
28
+ # Get dimensions
29
+ height, width = img_array.shape[:2]
30
  crop_size = min(width, height)
31
+
32
+ # Calculate crop coordinates
33
  left = (width - crop_size) // 2
34
  top = (height - crop_size) // 2
35
+
36
+ # Perform the crop on numpy array
37
+ img_cropped = img_array[top:top+crop_size, left:left+crop_size]
38
+
39
+ # Convert back to PIL and resize
40
+ img_pil = Image.fromarray(img_cropped)
41
+ return img_pil.resize((target_size, target_size), Image.Resampling.LANCZOS)
42
 
43
  def duplicate_horizontally(img):
44
  # Convert PIL Image to numpy array