Sean Powell commited on
Commit
b0b8c79
·
1 Parent(s): d07eaa9

Try a simpler process where the canvas is scaled, rotated and cropped square.

Browse files
experiments/large-diamond-crop-img2img.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils import pipes, filenames, tiling, images, wallpaper
2
+
3
+ prompt = "seamless pattern of parrots. black and white, drawing, white background, seamless, ornament."
4
+ desired_output_width = 512
5
+ inference_steps = 50
6
+ img2img_strength = 0.5
7
+ generation_dir = "./generations/large_diamond_crop_img2img"
8
+
9
+ sdxl_pipe = pipes.create_stable_diffusion_xl_pipeline("mps")
10
+ sdxl_img2img_pipe = pipes.create_stable_diffusion_xl_img2img_pipe("mps")
11
+
12
+ # Sources:
13
+ # - https://aituts.com/midjourney-tile/
14
+ prompts = [
15
+ "dog pattern children's bedroom wallpaper",
16
+ "dog pattern repeat realistic border collie stickers repeating pattern",
17
+ "pattern sago palm watercolor, white background, in the style of Rory McEwen, centered on the frame, single leaf",
18
+ "botanical art, watercolor, white background, no background, in the style of Pandora Sellars",
19
+ "Clip Art cityscape, vibrant colors and geometric patterns, a yellow background, designed by Herge, lava glow, Low angle",
20
+ "Craft a 4K visual masterpiece that envisions a surreal quantum garden, where reality blurs with the fantastical. Explore vibrant colors, fractal patterns, and intricate details to create a mesmerizing tapestry of interconnected dimensions. Let the scene evoke a sense of wonder and mystery as viewers explore the boundaries between the tangible and the ethereal",
21
+ "vibrant color grading, long exposure, bokeh, dramatic angle, extreme angle shot, horror, sci-fi",
22
+ ]
23
+
24
+ for prompt in prompts:
25
+ print(f"using prompt: `{prompt}`")
26
+ original_image_size = tiling.compute_input_tile_width_for_desired_output(desired_output_width)
27
+
28
+ original_image = sdxl_pipe(prompt=prompt, num_inference_steps=inference_steps, width=original_image_size,
29
+ height=original_image_size).images[0]
30
+ original_image_filename = filenames.generate_filename(f"{generation_dir}/originals", prompt)
31
+ original_image.save(original_image_filename)
32
+ print("generated original image", original_image_filename)
33
+
34
+ tiled_canvas = wallpaper.prepare_for_diamond_drop(original_image)
35
+ tiled_canvas_filename = filenames.generate_filename(f"{generation_dir}/tiled-canvas", prompt)
36
+ tiled_canvas.save(tiled_canvas_filename)
37
+ print("generated tiled_canvas", original_image_filename)
38
+
39
+ inner_rotated_tile = images.extract_larger_inner_rotated_tile_from_image(tiled_canvas, original_image_size,
40
+ original_image_size)
41
+ inner_rotated_tile_filename = filenames.generate_filename(f"{generation_dir}/inner_rotated_tiles", prompt,
42
+ "-inner-rotated")
43
+ inner_rotated_tile.save(inner_rotated_tile_filename)
44
+ print("generated inner rotated tile", inner_rotated_tile_filename)
45
+
46
+ half_drop = wallpaper.convert_tile_to_half_drop(inner_rotated_tile)
47
+ half_drop_filename = filenames.generate_filename(f"{generation_dir}/half_drops", prompt, "_half-drop-tile")
48
+ half_drop.save(half_drop_filename)
49
+ print("generated half drop tile", half_drop_filename)
50
+
51
+ half_drop_demo = wallpaper.generate_tile_test_arrangement(half_drop)
52
+ half_drop_demo_filename = filenames.generate_filename(f"{generation_dir}/half_drop_demos", prompt,
53
+ "_half-drop-demo")
54
+ half_drop_demo.save(half_drop_demo_filename)
55
+ print("generated half drop demo", half_drop_demo)
handler.py CHANGED
@@ -36,14 +36,11 @@ class EndpointHandler:
36
  original_image = \
37
  self.sdxl_pipe(prompt=prompt, num_inference_steps=text2img_inference_steps, width=original_image_size,
38
  height=original_image_size, guidance_scale=text2img_guidance_scale).images[0]
39
- inner_rotated_tile = images.extract_inner_rotated_tile_from_image(original_image)
40
-
41
- tileable_image = \
42
- self.sdxl_img2img_pipe(prompt, image=inner_rotated_tile.convert("RGB"),
43
- num_inference_steps=img2img_inference_steps,
44
- strength=img2img_strength).images[
45
- 0]
46
- half_drop = wallpaper.convert_tile_to_half_drop(tileable_image)
47
 
48
  half_drop_buffer = BytesIO()
49
  half_drop.save(half_drop_buffer, format="PNG")
 
36
  original_image = \
37
  self.sdxl_pipe(prompt=prompt, num_inference_steps=text2img_inference_steps, width=original_image_size,
38
  height=original_image_size, guidance_scale=text2img_guidance_scale).images[0]
39
+ tiled_canvas = wallpaper.prepare_for_diamond_drop(original_image)
40
+ inner_rotated_tile = images.extract_larger_inner_rotated_tile_from_image(tiled_canvas, original_image_size,
41
+ original_image_size)
42
+
43
+ half_drop = wallpaper.convert_tile_to_half_drop(inner_rotated_tile)
 
 
 
44
 
45
  half_drop_buffer = BytesIO()
46
  half_drop.save(half_drop_buffer, format="PNG")
utils/images.py CHANGED
@@ -50,3 +50,26 @@ def extract_inner_rotated_tile_from_image(original_image):
50
  cropped_square = rotated_diamond.crop((left, top, right, bottom))
51
 
52
  return cropped_square
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  cropped_square = rotated_diamond.crop((left, top, right, bottom))
51
 
52
  return cropped_square
53
+
54
+
55
+ def extract_larger_inner_rotated_tile_from_image(original_image, desired_width, desired_height):
56
+ """
57
+ Extract a diamond from the center of the square of size desired_width * desired_height and rotate 45° to convert to a square
58
+ """
59
+ width, height = original_image.size
60
+
61
+ # Rotate the image
62
+ rotated_image = original_image.rotate(-45, expand=False)
63
+
64
+ # Calculate dimensions of the inner square
65
+ side_length = int((width * math.sqrt(2) // 2))
66
+ print("side length", side_length)
67
+ left = (width - desired_width) // 2
68
+ top = (height - desired_height) // 2
69
+ right = width - left
70
+ bottom = height - top
71
+
72
+ # Crop out the inner square
73
+ cropped_square = rotated_image.crop((left, top, right, bottom))
74
+
75
+ return cropped_square
utils/wallpaper.py CHANGED
@@ -22,12 +22,13 @@ def convert_tile_to_half_drop(original_image):
22
  cropped_image = rotated_grid.crop(crop_box)
23
 
24
  # Crop the 724x1448 from the top-left
25
- new_height = round(math.sqrt(2 * size**2))
26
  new_width = round(new_height // 2)
27
  final_crop = cropped_image.crop((0, 0, new_width, new_height))
28
 
29
  return final_crop
30
 
 
31
  def generate_tile_test_arrangement(original_image):
32
  # Dimensions of the original image
33
  width, height = original_image.size
@@ -49,3 +50,25 @@ def generate_tile_test_arrangement(original_image):
49
  canvas.paste(original_image, (2 * width, (canvas_height - height) // 2))
50
 
51
  return canvas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  cropped_image = rotated_grid.crop(crop_box)
23
 
24
  # Crop the 724x1448 from the top-left
25
+ new_height = round(math.sqrt(2 * size ** 2))
26
  new_width = round(new_height // 2)
27
  final_crop = cropped_image.crop((0, 0, new_width, new_height))
28
 
29
  return final_crop
30
 
31
+
32
  def generate_tile_test_arrangement(original_image):
33
  # Dimensions of the original image
34
  width, height = original_image.size
 
50
  canvas.paste(original_image, (2 * width, (canvas_height - height) // 2))
51
 
52
  return canvas
53
+
54
+
55
+ def prepare_for_diamond_drop(original_image):
56
+ width, height = original_image.size
57
+
58
+ canvas_width = width * 2
59
+ canvas_height = height * 2
60
+
61
+ canvas = Image.new('RGB', (canvas_width, canvas_height), (255, 255, 255))
62
+
63
+ # Column 1
64
+ canvas.paste(original_image, (0, 0))
65
+ canvas.paste(original_image, (0, height))
66
+
67
+ # Column 2
68
+ canvas.paste(original_image, (width, 0))
69
+ canvas.paste(original_image, (width, height))
70
+
71
+ return canvas
72
+
73
+ # repeat the image, 2x2.
74
+ # crop from the center, 1x1.