John6666 commited on
Commit
dc1b7f5
1 Parent(s): 42c14ae

Upload mod.py

Browse files
Files changed (1) hide show
  1. mod.py +20 -4
mod.py CHANGED
@@ -46,6 +46,7 @@ control_scales = [0] * num_cns
46
  last_model = models[0]
47
  last_cn_on = False
48
 
 
49
  def is_repo_name(s):
50
  import re
51
  return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
@@ -116,6 +117,20 @@ def change_base_model(repo_id: str, cn_on: bool, progress=gr.Progress(track_tqdm
116
  return gr.update(visible=True)
117
 
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  # https://huggingface.co/spaces/DamarJati/FLUX.1-DEV-Canny/blob/main/app.py
120
  def resize_image(image, target_width, target_height, crop=True):
121
  from image_datasets.canny_dataset import c_crop
@@ -169,8 +184,9 @@ def get_control_params():
169
 
170
  from preprocessor import Preprocessor
171
  def preprocess_image(image: Image.Image, control_mode: str, height: int, width: int, preprocess_resolution: int):
 
172
  image_resolution = max(width, height)
173
- image_before = resize_image(image, image_resolution, image_resolution, True)
174
  # generated control_
175
  print("start to generate control image")
176
  preprocessor = Preprocessor()
@@ -213,15 +229,15 @@ def preprocess_image(image: Image.Image, control_mode: str, height: int, width:
213
  detect_resolution=preprocess_resolution,
214
  )
215
 
216
- if control_mode == "low_quality" or control_mode == "gray" or control_mode == "blur" or control_mode == "tile":
217
  control_image = image_before
218
  image_width = 768
219
  image_height = 768
220
  else:
221
  # make sure control image size is same as resized_image
222
- image_width, image_height = control_image.size
223
 
224
- image_after = resize_image(control_image, width, height, True)
225
  print(f"generate control image success: {image_width}x{image_height} => {width}x{height}")
226
 
227
  return image_after
 
46
  last_model = models[0]
47
  last_cn_on = False
48
 
49
+
50
  def is_repo_name(s):
51
  import re
52
  return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
 
117
  return gr.update(visible=True)
118
 
119
 
120
+ def expand2square(pil_img: Image.Image, background_color: tuple=(0, 0, 0)):
121
+ width, height = pil_img.size
122
+ if width == height:
123
+ return pil_img
124
+ elif width > height:
125
+ result = Image.new(pil_img.mode, (width, width), background_color)
126
+ result.paste(pil_img, (0, (width - height) // 2))
127
+ return result
128
+ else:
129
+ result = Image.new(pil_img.mode, (height, height), background_color)
130
+ result.paste(pil_img, ((height - width) // 2, 0))
131
+ return result
132
+
133
+
134
  # https://huggingface.co/spaces/DamarJati/FLUX.1-DEV-Canny/blob/main/app.py
135
  def resize_image(image, target_width, target_height, crop=True):
136
  from image_datasets.canny_dataset import c_crop
 
184
 
185
  from preprocessor import Preprocessor
186
  def preprocess_image(image: Image.Image, control_mode: str, height: int, width: int, preprocess_resolution: int):
187
+ if control_mode == "None": return image
188
  image_resolution = max(width, height)
189
+ image_before = resize_image(expand2square(image), image_resolution, image_resolution, False)
190
  # generated control_
191
  print("start to generate control image")
192
  preprocessor = Preprocessor()
 
229
  detect_resolution=preprocess_resolution,
230
  )
231
 
232
+ if control_mode == "low_quality" or control_mode == "gray" or control_mode == "blur" or control_mode == "tile":
233
  control_image = image_before
234
  image_width = 768
235
  image_height = 768
236
  else:
237
  # make sure control image size is same as resized_image
238
+ image_width, image_height = control_image.size
239
 
240
+ image_after = resize_image(control_image, width, height, False)
241
  print(f"generate control image success: {image_width}x{image_height} => {width}x{height}")
242
 
243
  return image_after