philippemo commited on
Commit
585f3ad
1 Parent(s): 1942ea5

Add logging

Browse files
Files changed (1) hide show
  1. extract_images.py +13 -2
extract_images.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import argparse
3
  from io import BytesIO
4
  from PIL import Image
@@ -8,6 +9,12 @@ import dask.dataframe as dd
8
 
9
  dask.config.set({"dataframe.convert-string": False})
10
 
 
 
 
 
 
 
11
 
12
  def save_image(row, image_column, format, save_folder, print_logs):
13
  row_id = row.name
@@ -18,9 +25,9 @@ def save_image(row, image_column, format, save_folder, print_logs):
18
  img_path = os.path.join(save_folder, f'image_{row_id}.{format}')
19
  img.save(img_path)
20
  if print_logs:
21
- print(f"Saving image to {img_path}")
22
  except Exception as e:
23
- return f'Error saving image for row {row_id}: {str(e)}'
24
 
25
 
26
  def main():
@@ -50,6 +57,8 @@ def main():
50
 
51
  ddf = dd.read_parquet(args.parquet_file)
52
 
 
 
53
  results = ddf.apply(save_image,
54
  axis=1,
55
  meta=(args.image_column, "str"),
@@ -59,6 +68,8 @@ def main():
59
  print_logs=args.print_logs)
60
  results.compute()
61
 
 
 
62
 
63
  if __name__ == "__main__":
64
  main()
 
1
  import os
2
+ import logging
3
  import argparse
4
  from io import BytesIO
5
  from PIL import Image
 
9
 
10
  dask.config.set({"dataframe.convert-string": False})
11
 
12
+ logging.basicConfig(
13
+ format="[%(asctime)s | %(name)s | %(levelname)s] %(message)s",
14
+ level=logging.INFO,
15
+ )
16
+ logger = logging.getLogger(__name__)
17
+
18
 
19
  def save_image(row, image_column, format, save_folder, print_logs):
20
  row_id = row.name
 
25
  img_path = os.path.join(save_folder, f'image_{row_id}.{format}')
26
  img.save(img_path)
27
  if print_logs:
28
+ logger.info(f"Saving image to {img_path}")
29
  except Exception as e:
30
+ logger.warning(f'Error saving image for row {row_id}: {str(e)}')
31
 
32
 
33
  def main():
 
57
 
58
  ddf = dd.read_parquet(args.parquet_file)
59
 
60
+ logger.info(f"Extracting images to folder `{args.save_folder}`")
61
+
62
  results = ddf.apply(save_image,
63
  axis=1,
64
  meta=(args.image_column, "str"),
 
68
  print_logs=args.print_logs)
69
  results.compute()
70
 
71
+ logger.info("Image extraction complete")
72
+
73
 
74
  if __name__ == "__main__":
75
  main()