pad image before resize

#7
by radames - opened
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -66,6 +66,19 @@ def count_files(*inputs):
66
  Training_Steps = file_counter*200
67
  return(gr.update(visible=True, value=f"You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps. This should take around {round(Training_Steps/1.5, 2)} seconds, or {round((Training_Steps/1.5)/3600, 2)} hours. As a reminder, the T4 GPU costs US$0.60 for 1h. Once training is over, don't forget to swap the hardware back to CPU."))
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def train(*inputs):
70
  if "IS_SHARED_UI" in os.environ:
71
  raise gr.Error("This Space only works in duplicated instances")
@@ -84,13 +97,7 @@ def train(*inputs):
84
  raise gr.Error("You forgot to define your concept prompt")
85
  for j, file_temp in enumerate(files):
86
  file = Image.open(file_temp.name)
87
- width, height = file.size
88
- side_length = min(width, height)
89
- left = (width - side_length)/2
90
- top = (height - side_length)/2
91
- right = (width + side_length)/2
92
- bottom = (height + side_length)/2
93
- image = file.crop((left, top, right, bottom))
94
  image = image.resize((512, 512))
95
  extension = file_temp.name.split(".")[1]
96
  image = image.convert('RGB')
 
66
  Training_Steps = file_counter*200
67
  return(gr.update(visible=True, value=f"You are going to train {concept_counter} {type_of_thing}(s), with {file_counter} images for {Training_Steps} steps. This should take around {round(Training_Steps/1.5, 2)} seconds, or {round((Training_Steps/1.5)/3600, 2)} hours. As a reminder, the T4 GPU costs US$0.60 for 1h. Once training is over, don't forget to swap the hardware back to CPU."))
68
 
69
+ def pad_image(image):
70
+ w, h = image.size
71
+ if w == h:
72
+ return image
73
+ elif w > h:
74
+ new_image = Image.new(image.mode, (w, w), (0, 0, 0))
75
+ new_image.paste(image, (0, (w - h) // 2))
76
+ return new_image
77
+ else:
78
+ new_image = Image.new(image.mode, (h, h), (0, 0, 0))
79
+ new_image.paste(image, ((h - w) // 2, 0))
80
+ return new_image
81
+
82
  def train(*inputs):
83
  if "IS_SHARED_UI" in os.environ:
84
  raise gr.Error("This Space only works in duplicated instances")
 
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
+ image = pad_image(file)
 
 
 
 
 
 
101
  image = image.resize((512, 512))
102
  extension = file_temp.name.split(".")[1]
103
  image = image.convert('RGB')