fragger246 commited on
Commit
c9beab1
1 Parent(s): bac49e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -4,14 +4,11 @@ from PIL import Image
4
  import numpy as np
5
  import cv2
6
  from diffusers import StableDiffusionPipeline
7
- from huggingface_hub import login
8
-
9
-
10
 
11
  # Setup the model
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model_id = "s3nh/artwork-arcane-stable-diffusion"
14
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32, use_auth_token=True)
15
  pipe = pipe.to(device)
16
 
17
  # Generate T-shirt design function
@@ -30,6 +27,34 @@ def remove_background(design_image):
30
  design_np = cv2.merge(rgba, 4)
31
  return design_np
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # T-shirt mockup generator with Gradio interface
34
  examples = [
35
  ["MyBrand"],
@@ -76,28 +101,8 @@ with gr.Blocks(css=css) as demo:
76
  # Load blank T-shirt mockup template image
77
  mockup_template = Image.open("/content/drive/MyDrive/unnamed.jpg")
78
 
79
- # Convert mockup template to numpy array
80
- mockup_np = np.array(mockup_template)
81
-
82
- # Resize design image to fit mockup
83
- design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
84
-
85
- # Center the design on the mockup
86
- y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
87
- x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
88
- y1, y2 = y_offset, y_offset + design_resized.shape[0]
89
- x1, x2 = x_offset, x_offset + design_resized.shape[1]
90
-
91
- # Blend design with mockup using alpha channel
92
- alpha_s = design_resized[:, :, 3] / 255.0 if design_resized.shape[2] == 4 else np.ones(design_resized.shape[:2])
93
- alpha_l = 1.0 - alpha_s
94
-
95
- for c in range(0, 3):
96
- mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
97
- alpha_l * mockup_np[y1:y2, x1:x2, c])
98
-
99
- # Convert back to PIL image for Gradio output
100
- result_image = Image.fromarray(mockup_np)
101
 
102
  return result_image
103
 
@@ -107,4 +112,4 @@ with gr.Blocks(css=css) as demo:
107
  outputs=[result]
108
  )
109
 
110
- demo.queue().launch()
 
4
  import numpy as np
5
  import cv2
6
  from diffusers import StableDiffusionPipeline
 
 
 
7
 
8
  # Setup the model
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model_id = "s3nh/artwork-arcane-stable-diffusion"
11
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
12
  pipe = pipe.to(device)
13
 
14
  # Generate T-shirt design function
 
27
  design_np = cv2.merge(rgba, 4)
28
  return design_np
29
 
30
+ # Blend design with T-shirt mockup
31
+ def blend_design_with_mockup(mockup, design):
32
+ mockup_np = np.array(mockup)
33
+ design_np = np.array(design)
34
+
35
+ # Ensure the design is RGBA
36
+ if design_np.shape[2] == 3:
37
+ alpha = np.ones((design_np.shape[0], design_np.shape[1], 1), dtype=design_np.dtype) * 255
38
+ design_np = np.concatenate((design_np, alpha), axis=2)
39
+
40
+ design_resized = cv2.resize(design_np, (mockup_np.shape[1] // 4, mockup_np.shape[0] // 4)) # Adjust size as needed
41
+
42
+ y_offset = (mockup_np.shape[0] - design_resized.shape[0]) // 2
43
+ x_offset = (mockup_np.shape[1] - design_resized.shape[1]) // 2
44
+
45
+ y1, y2 = y_offset, y_offset + design_resized.shape[0]
46
+ x1, x2 = x_offset, x_offset + design_resized.shape[1]
47
+
48
+ alpha_s = design_resized[:, :, 3] / 255.0
49
+ alpha_l = 1.0 - alpha_s
50
+
51
+ for c in range(0, 3):
52
+ mockup_np[y1:y2, x1:x2, c] = (alpha_s * design_resized[:, :, c] +
53
+ alpha_l * mockup_np[y1:y2, x1:x2, c])
54
+
55
+ result_image = Image.fromarray(mockup_np)
56
+ return result_image
57
+
58
  # T-shirt mockup generator with Gradio interface
59
  examples = [
60
  ["MyBrand"],
 
101
  # Load blank T-shirt mockup template image
102
  mockup_template = Image.open("/content/drive/MyDrive/unnamed.jpg")
103
 
104
+ # Blend design with mockup
105
+ result_image = blend_design_with_mockup(mockup_template, Image.fromarray(design_np))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  return result_image
108
 
 
112
  outputs=[result]
113
  )
114
 
115
+ demo.queue().launch()