thomaseding
commited on
Commit
·
2a3e6a9
1
Parent(s):
7b59237
Change some arg names
Browse files- mapped_downscale.py +29 -29
mapped_downscale.py
CHANGED
@@ -235,54 +235,54 @@ def str2bool(value) -> bool:
|
|
235 |
raise argparse.ArgumentTypeError("Boolean value expected.")
|
236 |
|
237 |
|
238 |
-
def mapped_downscale(*, control_path: str, input_path: str,
|
239 |
"""
|
240 |
Downsample and rescale an image.
|
241 |
-
|
242 |
:param control_path: Path to the control image.
|
243 |
:param input_path: Path to the input image.
|
244 |
-
:param
|
245 |
-
:param
|
246 |
:param sample_radius: Radius for sampling (Manhattan distance).
|
247 |
:param downsampler: Downsampler to use.
|
248 |
:param trim_cropped_edges: Drop mapped checker grid elements that are cropped in the control image.
|
249 |
"""
|
|
|
|
|
|
|
250 |
control_image = Image.open(control_path).convert("1")
|
251 |
input_image = Image.open(input_path)
|
252 |
if control_image.size != input_image.size:
|
253 |
raise ValueError("Control image and input image must have the same dimensions.")
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
if
|
259 |
-
|
260 |
-
|
261 |
-
if output_up_path:
|
262 |
-
output_image = ImageRef(Image.new("RGB", input_image.size))
|
263 |
|
264 |
extracted_boxes = extract_boxes(control_image)
|
265 |
-
|
266 |
-
if
|
267 |
-
|
268 |
-
|
269 |
boxes = extracted_boxes.boxes()
|
270 |
-
downsample_all(input_image=input_image, output_image=
|
271 |
-
|
272 |
-
if
|
273 |
-
assert
|
274 |
-
|
275 |
-
if
|
276 |
-
assert
|
277 |
-
|
278 |
|
279 |
|
280 |
def main(cli_args: List[str]) -> None:
|
281 |
parser = argparse.ArgumentParser(description="Downsample and rescale image.")
|
282 |
parser.add_argument("--control", required=True, help="Path to control image.")
|
283 |
parser.add_argument("--input", required=True, help="Path to input image.")
|
284 |
-
parser.add_argument("--output-
|
285 |
-
parser.add_argument("--output-
|
286 |
parser.add_argument("--sample-radius", type=int, default=None, help="Radius for sampling (Manhattan distance).")
|
287 |
parser.add_argument("--downsampler", choices=["box", "bilinear", "bicubic", "hamming", "lanczos"], default="box", help="Downsampler to use.")
|
288 |
parser.add_argument("--trim-cropped-edges", type=str2bool, default=False, help="Drop mapped checker grid elements that are cropped in the control image.")
|
@@ -293,8 +293,8 @@ def main(cli_args: List[str]) -> None:
|
|
293 |
mapped_downscale(
|
294 |
control_path=args.control,
|
295 |
input_path=args.input,
|
296 |
-
|
297 |
-
|
298 |
sample_radius=args.sample_radius,
|
299 |
downsampler=downsampler,
|
300 |
trim_cropped_edges=args.trim_cropped_edges
|
|
|
235 |
raise argparse.ArgumentTypeError("Boolean value expected.")
|
236 |
|
237 |
|
238 |
+
def mapped_downscale(*, control_path: str, input_path: str, output_downscaled_path: Optional[str], output_quantized_path: Optional[str], sample_radius: Optional[int], downsampler: Image.Resampling, trim_cropped_edges: bool) -> None:
|
239 |
"""
|
240 |
Downsample and rescale an image.
|
241 |
+
|
242 |
:param control_path: Path to the control image.
|
243 |
:param input_path: Path to the input image.
|
244 |
+
:param output_downscaled_path: Path to save the output downscaled image.
|
245 |
+
:param output_quantized_path: Path to save the output quantized image (downscaled and then upscaled to the original size).
|
246 |
:param sample_radius: Radius for sampling (Manhattan distance).
|
247 |
:param downsampler: Downsampler to use.
|
248 |
:param trim_cropped_edges: Drop mapped checker grid elements that are cropped in the control image.
|
249 |
"""
|
250 |
+
if not output_downscaled_path and not output_quantized_path:
|
251 |
+
raise ValueError("At least one of output_up and output_down must be specified.")
|
252 |
+
|
253 |
control_image = Image.open(control_path).convert("1")
|
254 |
input_image = Image.open(input_path)
|
255 |
if control_image.size != input_image.size:
|
256 |
raise ValueError("Control image and input image must have the same dimensions.")
|
257 |
+
|
258 |
+
downscaled_image: Optional[ImageRef] = None
|
259 |
+
quantized_image: Optional[ImageRef] = None
|
260 |
+
|
261 |
+
if output_quantized_path:
|
262 |
+
quantized_image = ImageRef(Image.new("RGB", input_image.size))
|
|
|
|
|
|
|
263 |
|
264 |
extracted_boxes = extract_boxes(control_image)
|
265 |
+
|
266 |
+
if output_downscaled_path:
|
267 |
+
downscaled_image = ImageRef(Image.new("RGB", extracted_boxes.down_dimensions()))
|
268 |
+
|
269 |
boxes = extracted_boxes.boxes()
|
270 |
+
downsample_all(input_image=input_image, output_image=quantized_image, down_image=downscaled_image, boxes=boxes, sample_radius=sample_radius, downsampler=downsampler, trim_cropped_edges=trim_cropped_edges)
|
271 |
+
|
272 |
+
if quantized_image:
|
273 |
+
assert output_quantized_path
|
274 |
+
quantized_image.ref.save(output_quantized_path)
|
275 |
+
if downscaled_image:
|
276 |
+
assert output_downscaled_path
|
277 |
+
downscaled_image.ref.save(output_downscaled_path)
|
278 |
|
279 |
|
280 |
def main(cli_args: List[str]) -> None:
|
281 |
parser = argparse.ArgumentParser(description="Downsample and rescale image.")
|
282 |
parser.add_argument("--control", required=True, help="Path to control image.")
|
283 |
parser.add_argument("--input", required=True, help="Path to input image.")
|
284 |
+
parser.add_argument("--output-downscaled", help="Path to save the output downscaled image.")
|
285 |
+
parser.add_argument("--output-quantized", help="Path to save the output quantized image (downscaled and then upscaled to the original size).")
|
286 |
parser.add_argument("--sample-radius", type=int, default=None, help="Radius for sampling (Manhattan distance).")
|
287 |
parser.add_argument("--downsampler", choices=["box", "bilinear", "bicubic", "hamming", "lanczos"], default="box", help="Downsampler to use.")
|
288 |
parser.add_argument("--trim-cropped-edges", type=str2bool, default=False, help="Drop mapped checker grid elements that are cropped in the control image.")
|
|
|
293 |
mapped_downscale(
|
294 |
control_path=args.control,
|
295 |
input_path=args.input,
|
296 |
+
output_downscaled_path=args.output_downscaled,
|
297 |
+
output_quantized_path=args.output_quantized,
|
298 |
sample_radius=args.sample_radius,
|
299 |
downsampler=downsampler,
|
300 |
trim_cropped_edges=args.trim_cropped_edges
|