throwaway74 commited on
Commit
931ade2
·
verified ·
1 Parent(s): 01fa978

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -19
app.py CHANGED
@@ -57,6 +57,31 @@ def fit_to_ratio(image: Image.Image, ratio_name: str, long_side: int) -> Image.I
57
  return image.resize((width, height), Image.LANCZOS)
58
 
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  # ---------------------------------
61
  # GPU function
62
  # ---------------------------------
@@ -65,11 +90,10 @@ def fit_to_ratio(image: Image.Image, ratio_name: str, long_side: int) -> Image.I
65
  def upscale(
66
  image,
67
  enhancer_type,
68
- tile_width,
69
- tile_height,
70
  use_ratio,
71
  ratio_name,
72
  target_long_side,
 
73
  ):
74
  if image is None:
75
  return None, None
@@ -79,13 +103,15 @@ def upscale(
79
  if use_ratio:
80
  img = fit_to_ratio(img, ratio_name, int(target_long_side))
81
 
 
 
82
  upscaler = get_upscaler(enhancer_type)
83
 
84
  out = upscaler(
85
  img,
86
  tiling=True,
87
- tile_width=int(tile_width),
88
- tile_height=int(tile_height),
89
  )
90
 
91
  if not isinstance(out, Image.Image):
@@ -117,19 +143,6 @@ with gr.Blocks() as demo:
117
  label="Enhancer Type"
118
  )
119
 
120
- with gr.Row():
121
- tile_width = gr.Number(
122
- value=1024,
123
- label="Tile Width",
124
- precision=0
125
- )
126
-
127
- tile_height = gr.Number(
128
- value=1024,
129
- label="Tile Height",
130
- precision=0
131
- )
132
-
133
  with gr.Group():
134
  use_ratio = gr.Checkbox(
135
  label="Use Aspect Ratio Preset",
@@ -148,6 +161,19 @@ with gr.Blocks() as demo:
148
  precision=0
149
  )
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  run_button = gr.Button("Enhance 4x")
152
 
153
  output_image = gr.Image(
@@ -160,16 +186,27 @@ with gr.Blocks() as demo:
160
  label="Download Full PNG"
161
  )
162
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  run_button.click(
164
  fn=upscale,
165
  inputs=[
166
  input_image,
167
  enhancer_type,
168
- tile_width,
169
- tile_height,
170
  use_ratio,
171
  ratio_name,
172
  target_long_side,
 
173
  ],
174
  outputs=[
175
  output_image,
 
57
  return image.resize((width, height), Image.LANCZOS)
58
 
59
 
60
+ def get_tile_dimensions(ratio_name: str, tile_preset: str):
61
+ long_side = int(tile_preset)
62
+ rw, rh = RATIO_MAP[ratio_name]
63
+
64
+ if rw >= rh:
65
+ tile_width = long_side
66
+ tile_height = round(long_side * rh / rw)
67
+ else:
68
+ tile_height = long_side
69
+ tile_width = round(long_side * rw / rh)
70
+
71
+ tile_width = max(2, tile_width - (tile_width % 2))
72
+ tile_height = max(2, tile_height - (tile_height % 2))
73
+
74
+ return tile_width, tile_height
75
+
76
+
77
+ def update_tile_display(ratio_name: str, tile_preset: str):
78
+ tile_width, tile_height = get_tile_dimensions(ratio_name, tile_preset)
79
+ return (
80
+ f"**Tile Width:** {tile_width}px \n"
81
+ f"**Tile Height:** {tile_height}px"
82
+ )
83
+
84
+
85
  # ---------------------------------
86
  # GPU function
87
  # ---------------------------------
 
90
  def upscale(
91
  image,
92
  enhancer_type,
 
 
93
  use_ratio,
94
  ratio_name,
95
  target_long_side,
96
+ tile_preset,
97
  ):
98
  if image is None:
99
  return None, None
 
103
  if use_ratio:
104
  img = fit_to_ratio(img, ratio_name, int(target_long_side))
105
 
106
+ tile_width, tile_height = get_tile_dimensions(ratio_name, tile_preset)
107
+
108
  upscaler = get_upscaler(enhancer_type)
109
 
110
  out = upscaler(
111
  img,
112
  tiling=True,
113
+ tile_width=tile_width,
114
+ tile_height=tile_height,
115
  )
116
 
117
  if not isinstance(out, Image.Image):
 
143
  label="Enhancer Type"
144
  )
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  with gr.Group():
147
  use_ratio = gr.Checkbox(
148
  label="Use Aspect Ratio Preset",
 
161
  precision=0
162
  )
163
 
164
+ with gr.Group():
165
+ gr.Markdown("### Tile Size Preset")
166
+
167
+ tile_preset = gr.Radio(
168
+ choices=["512", "768", "1024"],
169
+ value="768",
170
+ label="Preset Size"
171
+ )
172
+
173
+ tile_display = gr.Markdown(
174
+ value=update_tile_display("1:1", "768")
175
+ )
176
+
177
  run_button = gr.Button("Enhance 4x")
178
 
179
  output_image = gr.Image(
 
186
  label="Download Full PNG"
187
  )
188
 
189
+ ratio_name.change(
190
+ fn=update_tile_display,
191
+ inputs=[ratio_name, tile_preset],
192
+ outputs=tile_display
193
+ )
194
+
195
+ tile_preset.change(
196
+ fn=update_tile_display,
197
+ inputs=[ratio_name, tile_preset],
198
+ outputs=tile_display
199
+ )
200
+
201
  run_button.click(
202
  fn=upscale,
203
  inputs=[
204
  input_image,
205
  enhancer_type,
 
 
206
  use_ratio,
207
  ratio_name,
208
  target_long_side,
209
+ tile_preset,
210
  ],
211
  outputs=[
212
  output_image,