Parcourir la source

add of image converter

Jérôme BUISINE il y a 4 ans
Parent
commit
da1218b47c
2 fichiers modifiés avec 124 ajouts et 0 suppressions
  1. 58 0
      utils/convert_folder.sh
  2. 66 0
      utils/get_center_image.py

+ 58 - 0
utils/convert_folder.sh

@@ -0,0 +1,58 @@
+#! /bin/bash
+
+if [ -z "$1" ]
+  then
+    echo "No argument supplied"
+    echo "Need data folder"
+    exit 1
+fi
+
+if [ -z "$2" ]
+  then
+    echo "No argument supplied"
+    echo "Need output folder"
+    exit 1
+fi
+
+if [ -z "$3" ]
+  then
+    echo "No argument supplied"
+    echo "Need width size"
+    exit 1
+fi
+
+if [ -z "$4" ]
+  then
+    echo "No argument supplied"
+    echo "Need height size"
+    exit 1
+fi
+
+prefix="p3d_"
+
+data_folder=$1
+output_folder=$2
+
+width=$3
+height=$4
+
+for folder_path in $(ls -d ${data_folder}*)
+do
+    IFS='/' read -ra ADDR <<< "${folder_path}"
+    folder=${ADDR[-1]}
+
+    if [[ "$folder" == ${prefix}* ]]; then
+
+        output_scene_folder=$output_folder/${folder}_${width}_${height}
+        mkdir -p $output_scene_folder
+
+        for file in $(ls ${folder_path}*)
+        do
+            filepath=$folder_path/$file
+
+            python utils/get_center_image.py --image ${filepath} --size "${width},${height}" --output $output_scene_folder/$file
+            echo "Images centered saved into $output_scene_folder/$file"
+        done
+
+    fi
+done

+ 66 - 0
utils/get_center_image.py

@@ -0,0 +1,66 @@
+# main imports
+import os, sys
+import argparse
+import numpy as np
+
+# image processing
+from PIL import Image
+
+def reduce_image(image, size):
+    """Reduce image from its center
+    
+    Arguments:
+        image {PIL} -- PIL image expected
+        size {tuple(int,int)} -- tuple of 2 elements int (width, height)
+    """
+
+    image = np.array(image)
+
+    width, heigth, _ = image.shape
+    n_w, n_h = size # new expected size
+
+    # get center of image
+    middle_w = int(width / 2)
+    middle_h = int(heigth / 2)
+
+    # start coordinates
+    s_w = middle_w - int(n_w / 2)
+    s_h = middle_h - int(n_h / 2)
+
+    # end coordinates
+    e_w = middle_w + int(n_w / 2)
+    e_h = middle_h + int(n_h / 2)
+
+    return image[s_w:e_w, s_h:e_h]
+
+
+def main():
+    """
+    main function which is ran when launching script
+    """ 
+    parser = argparse.ArgumentParser(description="Reduce an image from its center coordinate")
+
+    parser.add_argument('--image', type=str, help='image to convert')
+    parser.add_argument('--size', type=str, help='size of expected output image (width, height)')
+    parser.add_argument('--output', type=str, help='output image filename')
+
+    args = parser.parse_args()
+
+    p_image  = args.image
+    p_size   = list(map(int, args.size.split(',')))
+    p_output = args.output
+
+
+    image = Image.open(p_image)
+
+    # get reduced image
+    reduced = reduce_image(image, p_size)
+
+    # save image
+    reduced = Image.fromarray(reduced)
+
+    reduced.save(p_output)
+
+
+if __name__ == "__main__":
+    main()