ApplyDesign commited on
Commit
c44925a
·
verified ·
1 Parent(s): c861252

Create upscaler.py

Browse files
Files changed (1) hide show
  1. upscaler.py +128 -0
upscaler.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ from diffusers import StableDiffusionUpscalePipeline
3
+ import torch
4
+ from split_image import split
5
+ import os
6
+ import random
7
+
8
+
9
+ def split_image(im, rows, cols, should_square, should_quiet=False):
10
+ im_width, im_height = im.size
11
+ row_width = int(im_width / cols)
12
+ row_height = int(im_height / rows)
13
+ name = "image"
14
+ ext = ".png"
15
+ name = os.path.basename(name)
16
+ images = []
17
+ if should_square:
18
+ min_dimension = min(im_width, im_height)
19
+ max_dimension = max(im_width, im_height)
20
+ if not should_quiet:
21
+ print("Resizing image to a square...")
22
+ print("Determining background color...")
23
+ bg_color = split.determine_bg_color(im)
24
+ if not should_quiet:
25
+ print("Background color is... " + str(bg_color))
26
+ im_r = Image.new("RGBA" if ext == "png" else "RGB",
27
+ (max_dimension, max_dimension), bg_color)
28
+ offset = int((max_dimension - min_dimension) / 2)
29
+ if im_width > im_height:
30
+ im_r.paste(im, (0, offset))
31
+ else:
32
+ im_r.paste(im, (offset, 0))
33
+ im = im_r
34
+ row_width = int(max_dimension / cols)
35
+ row_height = int(max_dimension / rows)
36
+ n = 0
37
+ for i in range(0, rows):
38
+ for j in range(0, cols):
39
+ box = (j * row_width, i * row_height, j * row_width +
40
+ row_width, i * row_height + row_height)
41
+ outp = im.crop(box)
42
+ outp_path = name + "_" + str(n) + ext
43
+ if not should_quiet:
44
+ print("Exporting image tile: " + outp_path)
45
+ images.append(outp)
46
+ n += 1
47
+ return [img for img in images]
48
+
49
+ def upscale_image(img, rows, cols,seed,prompt,negative_prompt,xformers,cpu_offload,attention_slicing,enable_custom_sliders=False,guidance=7,iterations=50):
50
+ model_id = "stabilityai/stable-diffusion-x4-upscaler"
51
+ try:
52
+ pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
53
+ except:
54
+ pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16, local_files_only=True)
55
+ pipeline = pipeline.to("cuda")
56
+ if xformers:
57
+ pipeline.enable_xformers_memory_efficient_attention()
58
+ else:
59
+ pipeline.disable_xformers_memory_efficient_attention()
60
+ if cpu_offload:
61
+ try:
62
+ pipeline.enable_sequential_cpu_offload()
63
+ except:
64
+ pass
65
+ if attention_slicing:
66
+ pipeline.enable_attention_slicing()
67
+ else:
68
+ pipeline.disable_attention_slicing()
69
+ img = Image.fromarray(img)
70
+ # load model and scheduler
71
+ if seed==-1:
72
+ generator = torch.manual_seed(random.randint(0, 9999999))
73
+ else:
74
+ generator = torch.manual_seed(seed)
75
+
76
+ original_width, original_height = img.size
77
+ max_dimension = max(original_width, original_height)
78
+ tiles = split_image(img, rows, cols, True, False)
79
+ ups_tiles = []
80
+ i = 0
81
+ for x in tiles:
82
+ i=i+1
83
+ if enable_custom_sliders:
84
+ ups_tile = pipeline(prompt=prompt,negative_prompt=negative_prompt,guidance_scale=guidance, num_inference_steps=iterations, image=x.convert("RGB"),generator=generator).images[0]
85
+ else:
86
+ ups_tile = pipeline(prompt=prompt,negative_prompt=negative_prompt, image=x.convert("RGB"),generator=generator).images[0]
87
+ ups_tiles.append(ups_tile)
88
+
89
+ # Determine the size of the merged upscaled image
90
+ total_width = 0
91
+ total_height = 0
92
+ side = 0
93
+ for ups_tile in ups_tiles:
94
+ side = ups_tile.width
95
+ break
96
+ for x in tiles:
97
+ tsize = x.width
98
+ break
99
+
100
+ ups_times = abs(side/tsize)
101
+ new_size = (max_dimension * ups_times, max_dimension * ups_times)
102
+ total_width = cols*side
103
+ total_height = rows*side
104
+
105
+ # Create a blank image with the calculated size
106
+ merged_image = Image.new("RGB", (total_width, total_height))
107
+
108
+ # Paste each upscaled tile into the blank image
109
+ current_width = 0
110
+ current_height = 0
111
+ maximum_width = cols*side
112
+ for ups_tile in ups_tiles:
113
+ merged_image.paste(ups_tile, (current_width, current_height))
114
+ current_width += ups_tile.width
115
+ if current_width>=maximum_width:
116
+ current_width = 0
117
+ current_height = current_height+side
118
+
119
+ # Using the center of the image as pivot, crop the image to the original dimension times four
120
+ crop_left = (new_size[0] - original_width * ups_times) // 2
121
+ crop_upper = (new_size[1] - original_height * ups_times) // 2
122
+ crop_right = crop_left + original_width * ups_times
123
+ crop_lower = crop_upper + original_height * ups_times
124
+ final_img = merged_image.crop((crop_left, crop_upper, crop_right, crop_lower))
125
+
126
+ # The resulting image should be identical to the original image in proportions / aspect ratio, with no loss of elements.
127
+ # Save the merged image
128
+ return final_img