hatmanstack commited on
Commit
fbfe6af
·
1 Parent(s): f00a818

outpainting black/white mask

Browse files
Files changed (3) hide show
  1. functions.py +11 -14
  2. image.png +0 -0
  3. mask.png +0 -0
functions.py CHANGED
@@ -39,22 +39,19 @@ def create_padded_image(image, padding_percent=100):
39
  def process_composite_to_mask(original_image, composite_image, transparent=False):
40
  original_array = np.array(original_image.convert('RGBA'))
41
  if transparent:
42
- white_background = Image.new('RGBA', original_image.size, (255, 255, 255, 255))
43
- white_background.paste(original_image, (0, 0), original_image)
44
- return white_background
45
  if composite_image is None:
46
- mask = np.full(original_array.shape[:2], 255, dtype=np.uint8) # Start with white
47
- transparent_areas = original_array[:, :, 3] == 0 # Alpha channel is 0 for transparent pixels
48
- mask[transparent_areas] = 0
49
  else:
50
  composite_array = np.array(composite_image.convert('RGBA'))
51
 
52
  difference = np.any(original_array != composite_array, axis=2)
53
  mask = np.full(original_array.shape[:2], 255, dtype=np.uint8)
54
  mask[difference] = 0
55
-
56
-
57
-
58
 
59
  return Image.fromarray(mask, mode='L')
60
 
@@ -127,8 +124,9 @@ def inpainting(mask_image, mask_prompt=None, text=None, negative_text=None, heig
127
 
128
  def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, outpainting_mode="DEFAULT", height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
129
  image = process_and_encode_image(mask_image['background'])
130
- if len(value) < 200:
131
- return None, gr.update(visible=True, value=value)
 
132
 
133
  if mask_prompt and mask_image:
134
  raise ValueError("You must specify either maskPrompt or maskImage, but not both.")
@@ -141,9 +139,9 @@ def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, out
141
  image = process_and_encode_image(image)
142
  mask_image = process_and_encode_image(mask)
143
 
144
- # Prepare the outPaintingParams dictionary
145
  out_painting_params = {
146
  "image": image,
 
147
  **({"maskImage": mask_image} if mask_image not in [None, ""] else {}),
148
  **({"maskPrompt": mask_prompt} if mask_prompt not in [None, ""] else {}),
149
  **({"text": text} if text not in [None, ""] else {"text": " "}),
@@ -165,7 +163,6 @@ def image_variation(images, text=None, negative_text=None, similarity_strength=0
165
  return None, gr.update(visible=True, value=value)
166
  encoded_images.append(value)
167
 
168
- # Prepare the imageVariationParams dictionary
169
  image_variation_params = {
170
  "images": encoded_images,
171
  **({"similarityStrength": similarity_strength} if similarity_strength not in [None, ""] else {}),
@@ -183,7 +180,7 @@ def image_conditioning(condition_image, text, negative_text=None, control_mode="
183
 
184
  if len(condition_image_encoded) < 200:
185
  return None, gr.update(visible=True, value=condition_image_encoded)
186
- # Prepare the textToImageParams dictionary
187
  text_to_image_params = {
188
  "text": text,
189
  "controlMode": control_mode,
 
39
  def process_composite_to_mask(original_image, composite_image, transparent=False):
40
  original_array = np.array(original_image.convert('RGBA'))
41
  if transparent:
42
+ black_background = Image.new('RGBA', original_image.size, (0, 0, 0, 255))
43
+ black_background.paste(original_image, (0, 0), original_image)
44
+ return black_background
45
  if composite_image is None:
46
+ mask = np.full(original_array.shape[:2], 0, dtype=np.uint8)
47
+ transparent_areas = original_array[:, :, 3] == 0
48
+ mask[transparent_areas] = 255
49
  else:
50
  composite_array = np.array(composite_image.convert('RGBA'))
51
 
52
  difference = np.any(original_array != composite_array, axis=2)
53
  mask = np.full(original_array.shape[:2], 255, dtype=np.uint8)
54
  mask[difference] = 0
 
 
 
55
 
56
  return Image.fromarray(mask, mode='L')
57
 
 
124
 
125
  def outpainting(mask_image, mask_prompt=None, text=None, negative_text=None, outpainting_mode="DEFAULT", height=1024, width=1024, quality="standard", cfg_scale=8.0, seed=0):
126
  image = process_and_encode_image(mask_image['background'])
127
+ if len(image) < 200:
128
+ print(image)
129
+ return None, gr.update(visible=True, value=image)
130
 
131
  if mask_prompt and mask_image:
132
  raise ValueError("You must specify either maskPrompt or maskImage, but not both.")
 
139
  image = process_and_encode_image(image)
140
  mask_image = process_and_encode_image(mask)
141
 
 
142
  out_painting_params = {
143
  "image": image,
144
+ #"outpaintingMode": outpainting_mode, ## Malformed JSON Error
145
  **({"maskImage": mask_image} if mask_image not in [None, ""] else {}),
146
  **({"maskPrompt": mask_prompt} if mask_prompt not in [None, ""] else {}),
147
  **({"text": text} if text not in [None, ""] else {"text": " "}),
 
163
  return None, gr.update(visible=True, value=value)
164
  encoded_images.append(value)
165
 
 
166
  image_variation_params = {
167
  "images": encoded_images,
168
  **({"similarityStrength": similarity_strength} if similarity_strength not in [None, ""] else {}),
 
180
 
181
  if len(condition_image_encoded) < 200:
182
  return None, gr.update(visible=True, value=condition_image_encoded)
183
+
184
  text_to_image_params = {
185
  "text": text,
186
  "controlMode": control_mode,
image.png ADDED
mask.png ADDED