thomaseding commited on
Commit
7b59237
·
1 Parent(s): c1b91e0

Factor some code for library use.

Browse files
Files changed (1) hide show
  1. mapped_downscale.py +54 -26
mapped_downscale.py CHANGED
@@ -227,14 +227,56 @@ def downsample_all(*, input_image: Image.Image, output_image: Optional[ImageRef]
227
  def str2bool(value) -> bool:
228
  if isinstance(value, bool):
229
  return value
230
- if value.lower() in ("yes", "true", "t", "y", "1"):
231
  return True
232
- elif value.lower() in ("no", "false", "f", "n", "0"):
233
  return False
234
  else:
235
  raise argparse.ArgumentTypeError("Boolean value expected.")
236
 
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  def main(cli_args: List[str]) -> None:
239
  parser = argparse.ArgumentParser(description="Downsample and rescale image.")
240
  parser.add_argument("--control", required=True, help="Path to control image.")
@@ -246,31 +288,17 @@ def main(cli_args: List[str]) -> None:
246
  parser.add_argument("--trim-cropped-edges", type=str2bool, default=False, help="Drop mapped checker grid elements that are cropped in the control image.")
247
 
248
  args = parser.parse_args(cli_args)
249
-
250
- control_image = Image.open(args.control).convert("1")
251
- input_image = Image.open(args.input)
252
- if control_image.size != input_image.size:
253
- raise ValueError("Control image and input image must have the same dimensions.")
254
  downsampler = Image.Resampling[args.downsampler.upper()]
255
- output_image: Optional[ImageRef] = None
256
- down_image: Optional[ImageRef] = None
257
- if not args.output_up and not args.output_down:
258
- raise ValueError("At least one of --output-up and --output-down must be specified.")
259
- if args.output_up:
260
- output_image = ImageRef(Image.new("RGB", input_image.size))
261
- extracted_boxes = extract_boxes(control_image)
262
- if args.output_down:
263
- down_image = ImageRef(Image.new("RGB", extracted_boxes.down_dimensions()))
264
-
265
- boxes = extracted_boxes.boxes()
266
-
267
- print(args.trim_cropped_edges)
268
-
269
- downsample_all(input_image=input_image, output_image=output_image, down_image=down_image, boxes=boxes, sample_radius=args.sample_radius, downsampler=downsampler, trim_cropped_edges=args.trim_cropped_edges)
270
- if output_image:
271
- output_image.ref.save(args.output_up)
272
- if down_image:
273
- down_image.ref.save(args.output_down)
274
 
275
 
276
  if __name__ == "__main__":
 
227
  def str2bool(value) -> bool:
228
  if isinstance(value, bool):
229
  return value
230
+ if value.lower() in ("true", "1"):
231
  return True
232
+ elif value.lower() in ("false", "0"):
233
  return False
234
  else:
235
  raise argparse.ArgumentTypeError("Boolean value expected.")
236
 
237
 
238
+ def mapped_downscale(*, control_path: str, input_path: str, output_up_path: Optional[str], output_down_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_up_path: Path to save the output image, upscaled to the original size.
245
+ :param output_down_path: Path to save the output image, kept at the downsampled 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
+ 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
+ output_image: Optional[ImageRef] = None
256
+ down_image: Optional[ImageRef] = None
257
+
258
+ if not output_up_path and not output_down_path:
259
+ raise ValueError("At least one of output_up and output_down must be specified.")
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 output_down_path:
267
+ down_image = ImageRef(Image.new("RGB", extracted_boxes.down_dimensions()))
268
+
269
+ boxes = extracted_boxes.boxes()
270
+ downsample_all(input_image=input_image, output_image=output_image, down_image=down_image, boxes=boxes, sample_radius=sample_radius, downsampler=downsampler, trim_cropped_edges=trim_cropped_edges)
271
+
272
+ if output_image:
273
+ assert output_up_path
274
+ output_image.ref.save(output_up_path)
275
+ if down_image:
276
+ assert output_down_path
277
+ down_image.ref.save(output_down_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.")
 
288
  parser.add_argument("--trim-cropped-edges", type=str2bool, default=False, help="Drop mapped checker grid elements that are cropped in the control image.")
289
 
290
  args = parser.parse_args(cli_args)
 
 
 
 
 
291
  downsampler = Image.Resampling[args.downsampler.upper()]
292
+
293
+ mapped_downscale(
294
+ control_path=args.control,
295
+ input_path=args.input,
296
+ output_up_path=args.output_up,
297
+ output_down_path=args.output_down,
298
+ sample_radius=args.sample_radius,
299
+ downsampler=downsampler,
300
+ trim_cropped_edges=args.trim_cropped_edges
301
+ )
 
 
 
 
 
 
 
 
 
302
 
303
 
304
  if __name__ == "__main__":