multimodalart HF staff radames commited on
Commit
e2138ad
1 Parent(s): 148af15

pad image before resize (#7)

Browse files

- pad image before resize (c0aa7c402ddd6319f39ec1dfd69be713cccf8f00)


Co-authored-by: Radamés Ajna <[email protected]>

Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -72,6 +72,19 @@ def count_files(*inputs):
72
  The setup, compression and uploading the model can take up to 20 minutes.<br>As the T4-Small GPU costs US$0.60 for 1h, <span style="font-size: 120%"><b>the estimated cost for this training is US${round((((Training_Steps/1.1)/3600)+0.3+0.1)*0.60, 2)}.</b></span><br><br>
73
  If you check the box below the GPU attribution will automatically removed after training is done and the model is uploaded. If not, don't forget to come back here and swap the hardware back to CPU.<br><br>''')])
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def train(*inputs):
76
  torch.cuda.empty_cache()
77
  if 'pipe' in globals():
@@ -97,13 +110,7 @@ def train(*inputs):
97
  raise gr.Error("You forgot to define your concept prompt")
98
  for j, file_temp in enumerate(files):
99
  file = Image.open(file_temp.name)
100
- width, height = file.size
101
- side_length = min(width, height)
102
- left = (width - side_length)/2
103
- top = (height - side_length)/2
104
- right = (width + side_length)/2
105
- bottom = (height + side_length)/2
106
- image = file.crop((left, top, right, bottom))
107
  image = image.resize((512, 512))
108
  extension = file_temp.name.split(".")[1]
109
  image = image.convert('RGB')
 
72
  The setup, compression and uploading the model can take up to 20 minutes.<br>As the T4-Small GPU costs US$0.60 for 1h, <span style="font-size: 120%"><b>the estimated cost for this training is US${round((((Training_Steps/1.1)/3600)+0.3+0.1)*0.60, 2)}.</b></span><br><br>
73
  If you check the box below the GPU attribution will automatically removed after training is done and the model is uploaded. If not, don't forget to come back here and swap the hardware back to CPU.<br><br>''')])
74
 
75
+ def pad_image(image):
76
+ w, h = image.size
77
+ if w == h:
78
+ return image
79
+ elif w > h:
80
+ new_image = Image.new(image.mode, (w, w), (0, 0, 0))
81
+ new_image.paste(image, (0, (w - h) // 2))
82
+ return new_image
83
+ else:
84
+ new_image = Image.new(image.mode, (h, h), (0, 0, 0))
85
+ new_image.paste(image, ((h - w) // 2, 0))
86
+ return new_image
87
+
88
  def train(*inputs):
89
  torch.cuda.empty_cache()
90
  if 'pipe' in globals():
 
110
  raise gr.Error("You forgot to define your concept prompt")
111
  for j, file_temp in enumerate(files):
112
  file = Image.open(file_temp.name)
113
+ image = pad_image(file)
 
 
 
 
 
 
114
  image = image.resize((512, 512))
115
  extension = file_temp.name.split(".")[1]
116
  image = image.convert('RGB')