resize image to 512x512

#2
by pcuenq HF staff - opened
Files changed (1) hide show
  1. app.py +17 -0
app.py CHANGED
@@ -11,6 +11,18 @@ import cv2
11
  def create_key(seed=0):
12
  return jax.random.PRNGKey(seed)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def canny_filter(image):
15
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
16
  blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
@@ -27,6 +39,11 @@ pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained(
27
 
28
  def infer(prompts, negative_prompts, image):
29
  params["controlnet"] = controlnet_params
 
 
 
 
 
30
 
31
  num_samples = 1 #jax.device_count()
32
  rng = create_key(0)
 
11
  def create_key(seed=0):
12
  return jax.random.PRNGKey(seed)
13
 
14
+ def crop_and_resize(pilimg, size=512):
15
+ """
16
+ Will downsample or upsample as necessary.
17
+ """
18
+ width, height = pilimg.size
19
+ minsize = min(width, height)
20
+ x0 = (width - height) // 2 if width > height else 0
21
+ y0 = (height - width) // 2 if height > width else 0
22
+ pilimg = pilimg.crop((x0, y0, x0 + minsize, y0 + minsize))
23
+ pilimg = pilimg.resize((size, size), resample=Image.LANCZOS)
24
+ return pilimg
25
+
26
  def canny_filter(image):
27
  gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
28
  blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
 
39
 
40
  def infer(prompts, negative_prompts, image):
41
  params["controlnet"] = controlnet_params
42
+
43
+ # image is a numpy array, we'll convert to PIL to resize and back to numpy
44
+ image = Image.fromarray(image)
45
+ image = crop_and_resize(image)
46
+ image = np.array(image)
47
 
48
  num_samples = 1 #jax.device_count()
49
  rng = create_key(0)